From the course: A Start to Using Generative AI in .NET

Unlock the full course today

Join today to access over 24,000 courses taught by industry experts.

Error handling

Error handling

- [Instructor] Having a functioning chat bot is great, but if you're an experienced developer, you know that things don't always go as planned. Let's discuss how to detect and handle errors in our little chat application. Looking at the code, the first thing that pops out is that we are assuming that the environment variable OpenAI API key is set. If it's not, the code will throw an exception. Let's fix this. I'm going to change it so that it allows no values. And then we are going to say if string that is null or empty key, then we are going to do the thing, console.writeline, please set the OpenAI API key environment variable period. And then we're just going to quit the application because there's nothing we can do at this point. Okay, let's try it out. I'm going to change the environment variable slightly so we don't have this, run the application and seems like it works. We do have the error message that we wanted. Okay, let me change the environment variable back and let's go…

Contents