← Back
IAmTimCorey August 24, 2026 39m

Understanding Exception Handling in C# - throw, Stack Traces, and more

Read full transcript 31 segments
  1. Handling exceptions in C means more than Handling exceptions in C means more than just using try catch. Knowing how to just using try catch. Knowing how to just using try catch. Knowing how to read a stack trace, understanding that read a stack trace, understanding that read a stack trace, understanding that different types of exceptions, and different types of exceptions, and different types of exceptions, and keeping your application in a safe state keeping your application in a safe state keeping your application in a safe state are all often things people miss. Your are all often things people miss. Your are all often things people miss. Your application's health is important. Let's application's health is important. Let's application's health is important. Let's review how to protect it. Now, videos review how to protect it. Now, videos review how to protect it. Now, videos like these are paid for by people who like these are paid for by people who like these are paid for by people who purchase training at imtimcorey.com. purchase training at imtimcorey.com. purchase training at imtimcorey.com. If you're looking to improve your If you're looking to improve your If you're looking to improve your development skills, keep up with AI, or development skills, keep up with AI, or development skills, keep up with AI, or even just learn to have fun building even just learn to have fun building even just learn to have fun building games, head over to imtimcorey.com games, head over to imtimcorey.com games, head over to imtimcorey.com and check it out. And if you want the and check it out. And if you want the and check it out. And if you want the source code from today's video, click source code from today's video, click source code from today's video, click the link in the description. Finally, if the link in the description. Finally, if the link in the description. Finally, if you want to see something specific you want to see something specific you want to see something specific covered either here or in a paid course, covered either here or in a paid course, covered either here or in a paid course, head over to a newly revamped suggestion head over to a newly revamped suggestion head over to a newly revamped suggestion site at suggestions.imtimcorey.com site at suggestions.imtimcorey.com site at suggestions.imtimcorey.com to vote on upcoming topic. All right, to vote on upcoming topic. All right, to vote on upcoming topic. All right, we're going to jump in really quick we're going to jump in really quick we're going to jump in really quick today with a console app. Let's call today with a console app. Let's call today with a console app. Let's call this exception demo and we'll call it this exception demo and we'll call it this exception demo and we'll call it exception demo app exception demo app exception demo app and net 10. Really, this is just a a and net 10. Really, this is just a a and net 10. Really, this is just a a simple harness for us to do some some simple harness for us to do some some simple harness for us to do some some quick work. Now, what we're going to do quick work. Now, what we're going to do quick work. Now, what we're going to do right off the bat is I am going to right off the bat is I am going to right off the bat is I am going to let's just create an exception. So, I'm let's just create an exception. So, I'm let's just create an exception. So, I'm going to say um uh let's go with going to say um uh let's go with going to say um uh let's go with int.parse parse and we're going to parse int.parse parse and we're going to parse int.parse parse and we're going to parse what should be a number. So like one or what should be a number. So like one or what should be a number. So like one or two that's in a string format. int.p two that's in a string format. int.p two that's in a string format. int.p parse changes it over to an integer but parse changes it over to an integer but parse changes it over to an integer but we're going to say uh test which is we're going to say uh test which is we're going to say uh test which is absolutely not what will work like you

  2. absolutely not what will work like you absolutely not what will work like you cannot convert test into an integer. So cannot convert test into an integer. So cannot convert test into an integer. So this code right here will throw an this code right here will throw an this code right here will throw an exception. Now let's run this code. I am exception. Now let's run this code. I am exception. Now let's run this code. I am going to be running using without the going to be running using without the going to be running using without the debugger because I want to see the debugger because I want to see the debugger because I want to see the exception in the debugger. I or in the exception in the debugger. I or in the exception in the debugger. I or in the console, I don't want to see it pop up console, I don't want to see it pop up console, I don't want to see it pop up here. So, if we run this, we're here. So, if we run this, we're here. So, if we run this, we're immediately going to get an exception. immediately going to get an exception. immediately going to get an exception. And this exception here says unhandled And this exception here says unhandled And this exception here says unhandled exception. So, first off, this is the exception. So, first off, this is the exception. So, first off, this is the the message, the the basic information the message, the the basic information the message, the the basic information about the exception. By the way, if about the exception. By the way, if about the exception. By the way, if you're reading exceptions, you should you're reading exceptions, you should you're reading exceptions, you should know all these different parts and how know all these different parts and how know all these different parts and how they work together. Okay? So, this is they work together. Okay? So, this is they work together. Okay? So, this is just a generic message saying this is just a generic message saying this is just a generic message saying this is what happened. In this case, we did not what happened. In this case, we did not what happened. In this case, we did not handle the exception. Handling means we handle the exception. Handling means we handle the exception. Handling means we put it inside of a try catch. We catch put it inside of a try catch. We catch put it inside of a try catch. We catch the exception and we do something the exception and we do something the exception and we do something controlled. This is an uncontrolled controlled. This is an uncontrolled controlled. This is an uncontrolled exception which crashes the application. exception which crashes the application. exception which crashes the application. Now to be clear, sometimes you want Now to be clear, sometimes you want Now to be clear, sometimes you want unhandled exceptions. You do not always unhandled exceptions. You do not always unhandled exceptions. You do not always want to control every exception. If you want to control every exception. If you want to control every exception. If you have not anticipated something, then you have not anticipated something, then you have not anticipated something, then you want your application to crash. You want your application to crash. You want your application to crash. You don't want to just have it eat the error don't want to just have it eat the error don't want to just have it eat the error and keep going. Otherwise, it may be and keep going. Otherwise, it may be and keep going. Otherwise, it may be working in unexpected working in unexpected working in unexpected state, which can cause hackers to be state, which can cause hackers to be state, which can cause hackers to be able to get into your application, do able to get into your application, do able to get into your application, do things that you did not expect because things that you did not expect because things that you did not expect because your application is in an unexpected your application is in an unexpected your application is in an unexpected state. Okay, so that's unhandled. Next state. Okay, so that's unhandled. Next state. Okay, so that's unhandled. Next up, it says, what type of exception? The up, it says, what type of exception? The up, it says, what type of exception? The system.format

  3. system.format system.format exception. Okay, so this is telling us exception. Okay, so this is telling us exception. Okay, so this is telling us which type of exception because there's which type of exception because there's which type of exception because there's multiple different types and this way we multiple different types and this way we multiple different types and this way we can later anticipate, oh, this might be can later anticipate, oh, this might be can later anticipate, oh, this might be an exception that we're going to see. an exception that we're going to see. an exception that we're going to see. Therefore, we'll properly handle this Therefore, we'll properly handle this Therefore, we'll properly handle this particular type of exception. So, here particular type of exception. So, here particular type of exception. So, here is a message we could show the user is a message we could show the user is a message we could show the user potentially the input string test was potentially the input string test was potentially the input string test was not in a correct format. Okay, so this not in a correct format. Okay, so this not in a correct format. Okay, so this is the if you did the ex message, this is the if you did the ex message, this is the if you did the ex message, this would be the message part of it. And it would be the message part of it. And it would be the message part of it. And it says, hey, the input string test was not says, hey, the input string test was not says, hey, the input string test was not in a correct format. By the way, that's in a correct format. By the way, that's in a correct format. By the way, that's very very clear. The input string, which very very clear. The input string, which very very clear. The input string, which is test, here's the actual value is not is test, here's the actual value is not is test, here's the actual value is not in the correct format for what you were in the correct format for what you were in the correct format for what you were trying to do. The format was you're trying to do. The format was you're trying to do. The format was you're expecting a number. So read your expecting a number. So read your expecting a number. So read your exception message by the way. Now, next exception message by the way. Now, next exception message by the way. Now, next up, we have the stack trace. And what up, we have the stack trace. And what up, we have the stack trace. And what this does is it tells us how this was this does is it tells us how this was this does is it tells us how this was called. So if you go down to the very called. So if you go down to the very called. So if you go down to the very bottom, we can see program main and then bottom, we can see program main and then bottom, we can see program main and then it says dollar sign string array args.

  4. it says dollar sign string array args. it says dollar sign string array args. Basically saying we're in the the main Basically saying we're in the the main Basically saying we're in the the main method inside of the program.cs file and method inside of the program.cs file and method inside of the program.cs file and in and here's the path to that. in and here's the path to that. in and here's the path to that. CS file. So it's in C demos exception CS file. So it's in C demos exception CS file. So it's in C demos exception demo app/exception demo/program.cs demo app/exception demo/program.cs demo app/exception demo/program.cs and here is the line. So on line three and here is the line. So on line three and here is the line. So on line three is where the exception happened. So is where the exception happened. So is where the exception happened. So that's the the bottommost entry in our that's the the bottommost entry in our that's the the bottommost entry in our stack trace. stack trace. stack trace. Go up one level. So up one level is at Go up one level. So up one level is at Go up one level. So up one level is at system.int32.parse system.int32.parse system.int32.parse string. This is what was called. So line string. This is what was called. So line string. This is what was called. So line three here called this method the three here called this method the three here called this method the system.int32.parse system.int32.parse system.int32.parse and it's expecting a string s and that's and it's expecting a string s and that's and it's expecting a string s and that's what was called up one more at what was called up one more at what was called up one more at system.number.throw system.number.throw system.number.throw format exception tchar read only span of format exception tchar read only span of format exception tchar read only span of one value. So this is the actual one value. So this is the actual one value. So this is the actual location where the exception happened.

  5. location where the exception happened. location where the exception happened. But But But we don't really control that area. But we don't really control that area. But we don't really control that area. But what we do know is that this is where it what we do know is that this is where it what we do know is that this is where it happened in our code. And here are the happened in our code. And here are the happened in our code. And here are the calls it was making. Here's the entries calls it was making. Here's the entries calls it was making. Here's the entries it was making into code to dial into it was making into code to dial into it was making into code to dial into exactly where that happened. So our code exactly where that happened. So our code exactly where that happened. So our code called parse that code called parse that code called parse that code had an entry right here that said throw had an entry right here that said throw had an entry right here that said throw format exception. Now we don't have line format exception. Now we don't have line format exception. Now we don't have line numbers or anything like that because we numbers or anything like that because we numbers or anything like that because we don't control this file but this tells don't control this file but this tells don't control this file but this tells us exactly where the exception happened. us exactly where the exception happened. us exactly where the exception happened. Now if we control all of this it might Now if we control all of this it might Now if we control all of this it might be a little bit more valuable but be a little bit more valuable but be a little bit more valuable but anywhere that we control we can see how anywhere that we control we can see how anywhere that we control we can see how it steps into our code. Like this code it steps into our code. Like this code it steps into our code. Like this code called this code which called this code called this code which called this code called this code which called this code which had an exception. So that allows which had an exception. So that allows which had an exception. So that allows us to see where in our code the end us to see where in our code the end us to see where in our code the end result of the exception was. So the end result of the exception was. So the end result of the exception was. So the end result was the exception happened right result was the exception happened right result was the exception happened right here on line three. But that's not here on line three. But that's not here on line three. But that's not really where the exception happened.

  6. really where the exception happened. really where the exception happened. That's the end spot. That's where it That's the end spot. That's where it That's the end spot. That's where it landed when we finally had the landed when we finally had the landed when we finally had the exception. Now we didn't catch it, but exception. Now we didn't catch it, but exception. Now we didn't catch it, but that's where it landed and we actually that's where it landed and we actually that's where it landed and we actually get a message from. The message happens get a message from. The message happens get a message from. The message happens right here. right here. right here. So that's our stack trace. It's really So that's our stack trace. It's really So that's our stack trace. It's really valuable information. It allows you to valuable information. It allows you to valuable information. It allows you to track down not only where exactly the track down not only where exactly the track down not only where exactly the exception happened, but what it was exception happened, but what it was exception happened, but what it was trying to do, what are the what it trying to do, what are the what it trying to do, what are the what it called, which then what did that call, called, which then what did that call, called, which then what did that call, which then what did that call in order which then what did that call in order which then what did that call in order to finally see where the exception to finally see where the exception to finally see where the exception happened. Okay, so happened. Okay, so happened. Okay, so let's do the good citizen thing and let's do the good citizen thing and let's do the good citizen thing and we're going to wrap this in a try catch. we're going to wrap this in a try catch. we're going to wrap this in a try catch. So we're going to say snippet surround So we're going to say snippet surround So we're going to say snippet surround with try and this is try catch with try and this is try catch with try and this is try catch and let's do this. Let's do console and let's do this. Let's do console and let's do this. Let's do console right line. We'll say ex do um message right line. We'll say ex do um message right line. We'll say ex do um message which is just that message part that I I which is just that message part that I I which is just that message part that I I pointed out. Okay. And we're not going pointed out. Okay. And we're not going pointed out. Okay. And we're not going to throw for right now. We'll come back to throw for right now. We'll come back to throw for right now. We'll come back to that. So what does a try catch do?

  7. to that. So what does a try catch do? to that. So what does a try catch do? Well, it kind of does what you would Well, it kind of does what you would Well, it kind of does what you would expect it to do. It tries to do all the expect it to do. It tries to do all the expect it to do. It tries to do all the code inside those curly braces. But code inside those curly braces. But code inside those curly braces. But whenever it finds an exception instead whenever it finds an exception instead whenever it finds an exception instead of blowing up the application with an of blowing up the application with an of blowing up the application with an unhandled exception, it catches the unhandled exception, it catches the unhandled exception, it catches the exception and allows you to safely do exception and allows you to safely do exception and allows you to safely do something with it. In this case, we're something with it. In this case, we're something with it. In this case, we're going to print it out to the console and going to print it out to the console and going to print it out to the console and be done. Okay, let's just put a console be done. Okay, let's just put a console be done. Okay, let's just put a console right on the end. Um, right on the end. Um, right on the end. Um, uh, we are done. uh, we are done. uh, we are done. something like that. So we can see that something like that. So we can see that something like that. So we can see that the application runs. Oops, I didn't the application runs. Oops, I didn't the application runs. Oops, I didn't want to do that. I wanted to well that want to do that. I wanted to well that want to do that. I wanted to well that would have show us anyways, but we're would have show us anyways, but we're would have show us anyways, but we're going to start debugging. Control F5. going to start debugging. Control F5. going to start debugging. Control F5. Okay, the input string test was not in Okay, the input string test was not in Okay, the input string test was not in the correct format. That's the the correct format. That's the the correct format. That's the exception. And then we are done. So we exception. And then we are done. So we exception. And then we are done. So we do try catch and now our application do try catch and now our application do try catch and now our application does not crash does not crash does not crash which may feel like we're done but we're which may feel like we're done but we're which may feel like we're done but we're not done because this right here could not done because this right here could not done because this right here could be a very big security hole. [snorts] be a very big security hole. [snorts] be a very big security hole. [snorts] Okay, I want to emphasize this that you Okay, I want to emphasize this that you Okay, I want to emphasize this that you got to be very careful in here that you got to be very careful in here that you got to be very careful in here that you are not allow your application to just are not allow your application to just are not allow your application to just eat errors. Right now we're just eating eat errors. Right now we're just eating eat errors. Right now we're just eating an error. We're we're just putting on to an error. We're we're just putting on to an error. We're we're just putting on to the console but we keep going. So, let's the console but we keep going. So, let's the console but we keep going. So, let's just say we have um int uh my number just say we have um int uh my number just say we have um int uh my number equals zero. And then we're going to say equals zero. And then we're going to say equals zero. And then we're going to say my uh let's go with age. How about that?

  8. my uh let's go with age. How about that? my uh let's go with age. How about that? Equals instead of my number. A little Equals instead of my number. A little Equals instead of my number. A little clearer. Okay. So, we're going to say clearer. Okay. So, we're going to say clearer. Okay. So, we're going to say age. And then down here, we're going to age. And then down here, we're going to age. And then down here, we're going to say say say we'll do a string interpolation first. we'll do a string interpolation first. we'll do a string interpolation first. Uh, your age is Uh, your age is Uh, your age is age. age. age. Okay. Now, do you see the flaw here? Okay. Now, do you see the flaw here? Okay. Now, do you see the flaw here? Let's let's do even further. This Let's let's do even further. This Let's let's do even further. This happens a lot. You don't do anything. happens a lot. You don't do anything. happens a lot. You don't do anything. Catch. You just in fact, let's be clear. Catch. You just in fact, let's be clear. Catch. You just in fact, let's be clear. You just catch and don't do anything. You just catch and don't do anything. You just catch and don't do anything. This is eating the error. So, if we were This is eating the error. So, if we were This is eating the error. So, if we were to run this, we can run this with the to run this, we can run this with the to run this, we can run this with the just F5. Your age is zero. just F5. Your age is zero. just F5. Your age is zero. So, it tried to parse that value. We So, it tried to parse that value. We So, it tried to parse that value. We gave it a value of test and it failed. gave it a value of test and it failed. gave it a value of test and it failed. So we ate the error which means that So we ate the error which means that So we ate the error which means that something bad happened. It it nothing something bad happened. It it nothing something bad happened. It it nothing worked here when we expected something worked here when we expected something worked here when we expected something to work. But yet because we ate the to work. But yet because we ate the to work. But yet because we ate the error, it just kept going in the error, it just kept going in the error, it just kept going in the application. Well, now the age is an application. Well, now the age is an application. Well, now the age is an invalid age. And that might be a big invalid age. And that might be a big invalid age. And that might be a big deal for age because okay, it's age is deal for age because okay, it's age is deal for age because okay, it's age is zero instead of being, you know, my zero instead of being, you know, my zero instead of being, you know, my actual age. However, if that's something actual age. However, if that's something actual age. However, if that's something where you're expecting certain types of where you're expecting certain types of where you're expecting certain types of values, this could leave this in an values, this could leave this in an values, this could leave this in an unexpected state, which can be an an unexpected state, which can be an an unexpected state, which can be an an entry for hackers to manipulate your entry for hackers to manipulate your entry for hackers to manipulate your application, make it even worse because application, make it even worse because application, make it even worse because what if your application then does what if your application then does what if your application then does actions based upon age, maybe divides by actions based upon age, maybe divides by actions based upon age, maybe divides by your age for some reason, then you your age for some reason, then you your age for some reason, then you divide by zero and causing other

  9. divide by zero and causing other divide by zero and causing other exceptions and it can be a trickle down exceptions and it can be a trickle down exceptions and it can be a trickle down effect of making your application a effect of making your application a effect of making your application a mess. mess. mess. So, do not just catch the error and keep So, do not just catch the error and keep So, do not just catch the error and keep going. The goal is not to just stop going. The goal is not to just stop going. The goal is not to just stop exceptions. exceptions. exceptions. That's not the goal because this right That's not the goal because this right That's not the goal because this right here is stopped exceptions. And you may here is stopped exceptions. And you may here is stopped exceptions. And you may hear about global exception handlers. hear about global exception handlers. hear about global exception handlers. And I probably do a video on at some And I probably do a video on at some And I probably do a video on at some point on global exception handlers and point on global exception handlers and point on global exception handlers and how to globally um catch exceptions. how to globally um catch exceptions. how to globally um catch exceptions. That can be a good thing if you do it That can be a good thing if you do it That can be a good thing if you do it right. But if you don't, what it's doing right. But if you don't, what it's doing right. But if you don't, what it's doing is just stopping you from seeing actual is just stopping you from seeing actual is just stopping you from seeing actual exceptions. So don't do that. Make sure exceptions. So don't do that. Make sure exceptions. So don't do that. Make sure that you're doing some type of work in that you're doing some type of work in that you're doing some type of work in here. And in this case, we'll just say here. And in this case, we'll just say here. And in this case, we'll just say console right line ex. console right line ex. console right line ex. And then we're going to do a throw. And then we're going to do a throw. And then we're going to do a throw. Okay. So let's see what this does. And Okay. So let's see what this does. And Okay. So let's see what this does. And let's do the the um without debugging. let's do the the um without debugging. let's do the the um without debugging. Okay. So the input string test was not Okay. So the input string test was not Okay. So the input string test was not in the correct format. Okay, that was in the correct format. Okay, that was in the correct format. Okay, that was our our uh console.right lineex ex our our uh console.right lineex ex our our uh console.right lineex ex message. But then we have unhandled message. But then we have unhandled message. But then we have unhandled exception format exception. The input exception format exception. The input exception format exception. The input test was not in the correct format.

  10. test was not in the correct format. test was not in the correct format. Well, wait a minute here. Why is it that Well, wait a minute here. Why is it that Well, wait a minute here. Why is it that we're getting this message unhandled we're getting this message unhandled we're getting this message unhandled even though we did handle it? We had a even though we did handle it? We had a even though we did handle it? We had a try. We had a catch. Because of this try. We had a catch. Because of this try. We had a catch. Because of this throw right here, what this does is it throw right here, what this does is it throw right here, what this does is it says, "Hey, keep going." like says, "Hey, keep going." like says, "Hey, keep going." like even though I caught this exception, even though I caught this exception, even though I caught this exception, pretend I didn't and keep passing that pretend I didn't and keep passing that pretend I didn't and keep passing that up the chain because what happens is up the chain because what happens is up the chain because what happens is when you have an exception, it can when you have an exception, it can when you have an exception, it can process right where the exception process right where the exception process right where the exception happens if you're doing a try catch, but happens if you're doing a try catch, but happens if you're doing a try catch, but then you might want to process it later then you might want to process it later then you might want to process it later as well. So, let's do this. Let's um say as well. So, let's do this. Let's um say as well. So, let's do this. Let's um say control shift A and we're going to say control shift A and we're going to say control shift A and we're going to say uh my methods. This is a uh a public uh my methods. This is a uh a public uh my methods. This is a uh a public static class. static class. static class. And we're going to say uh public static And we're going to say uh public static And we're going to say uh public static int get age and we'll pass in a string int get age and we'll pass in a string int get age and we'll pass in a string age string. age string. age string. And we're going to pass back the value.

  11. And we're going to pass back the value. And we're going to pass back the value. So int output So int output So int output equals zero. And then I say equals z equals zero. And then I say equals z equals zero. And then I say equals z because there's something specific I'm because there's something specific I'm because there's something specific I'm trying to do here. Um, but we're going trying to do here. Um, but we're going trying to do here. Um, but we're going to say output equals int.parse to say output equals int.parse to say output equals int.parse and then age string and then return and then age string and then return and then age string and then return output. output. output. We know this is going to cause an We know this is going to cause an We know this is going to cause an exception if you pass in a bad value. By exception if you pass in a bad value. By exception if you pass in a bad value. By the way, I'm using int.parse when we the way, I'm using int.parse when we the way, I'm using int.parse when we have int. have int. have int. I know we have trip parse, but int.parse I know we have trip parse, but int.parse I know we have trip parse, but int.parse throws exception. I want that. So that's throws exception. I want that. So that's throws exception. I want that. So that's why we're doing it as an example. Okay. why we're doing it as an example. Okay. why we're doing it as an example. Okay. So get age if I come over here instead So get age if I come over here instead So get age if I come over here instead of age equals this I'm going to say um of age equals this I'm going to say um of age equals this I'm going to say um this is my methods.get this is my methods.get this is my methods.get age and we're passing test the bad age and we're passing test the bad age and we're passing test the bad value. value. value. So all we're doing is here making a call So all we're doing is here making a call So all we're doing is here making a call here to age and then we are um going to here to age and then we are um going to here to age and then we are um going to do console right line and throw. In fact do console right line and throw. In fact do console right line and throw. In fact we're we're not even going to do the we're we're not even going to do the we're we're not even going to do the console run. We're just going to say console run. We're just going to say console run. We're just going to say throw. So we're not actually doing throw. So we're not actually doing throw. So we're not actually doing anything in here. Um, let's run this and anything in here. Um, let's run this and anything in here. Um, let's run this and we're gonna do a uh start that we're gonna do a uh start that we're gonna do a uh start that debugging.

  12. debugging. debugging. Okay, we have an unhandled exception. Okay, we have an unhandled exception. Okay, we have an unhandled exception. The format exception. The input string The format exception. The input string The format exception. The input string is not in the correct format. Cool. And is not in the correct format. Cool. And is not in the correct format. Cool. And notice this happens on program.cs line notice this happens on program.cs line notice this happens on program.cs line 8. The next line up is it called 8. The next line up is it called 8. The next line up is it called the get age at here is the CS file line the get age at here is the CS file line the get age at here is the CS file line 13. and then it called int32.par string 13. and then it called int32.par string 13. and then it called int32.par string and then it called this line here that and then it called this line here that and then it called this line here that throws the exception. throws the exception. throws the exception. So this is our stack trace at work and So this is our stack trace at work and So this is our stack trace at work and we can see now how this goes through our we can see now how this goes through our we can see now how this goes through our application, how it moves through our application, how it moves through our application, how it moves through our application and where the actual application and where the actual application and where the actual exception happens. Okay, so now we can exception happens. Okay, so now we can exception happens. Okay, so now we can do is we can say well we need to wrap do is we can say well we need to wrap do is we can say well we need to wrap that a try parse or a try catch. So that a try parse or a try catch. So that a try parse or a try catch. So let's surround with a try catch and let's surround with a try catch and let's surround with a try catch and we're going to catch the exception like we're going to catch the exception like we're going to catch the exception like good citizens and we're going to say good citizens and we're going to say good citizens and we're going to say console write line. I'm going to say ex console write line. I'm going to say ex console write line. I'm going to say ex for a minute. Write the entire thing and for a minute. Write the entire thing and for a minute. Write the entire thing and then I'm going to do is I'm going to then I'm going to do is I'm going to then I'm going to do is I'm going to throw it and then we're going to you throw it and then we're going to you throw it and then we're going to you know return output. However, we're not know return output. However, we're not know return output. However, we're not returning output if we throw the returning output if we throw the returning output if we throw the exception. If the exception happens this exception. If the exception happens this exception. If the exception happens this is we end it right here. We don't is we end it right here. We don't is we end it right here. We don't actually get to the return statement.

  13. actually get to the return statement. actually get to the return statement. But now I'm throwing. And then over here But now I'm throwing. And then over here But now I'm throwing. And then over here I am catching. In fact, what we're going I am catching. In fact, what we're going I am catching. In fact, what we're going to do here is we're going to say console to do here is we're going to say console to do here is we're going to say console right line ex um the entire ex. Okay. In right line ex um the entire ex. Okay. In right line ex um the entire ex. Okay. In fact, let's do that. Uh we're doing that fact, let's do that. Uh we're doing that fact, let's do that. Uh we're doing that here too. Great. So we're going to catch here too. Great. So we're going to catch here too. Great. So we're going to catch it again here. And then yes, it's going it again here. And then yes, it's going it again here. And then yes, it's going to say your age is zero. That's okay for to say your age is zero. That's okay for to say your age is zero. That's okay for now. We just want to stop at this point. now. We just want to stop at this point. now. We just want to stop at this point. Okay. So let's start this again. And we Okay. So let's start this again. And we Okay. So let's start this again. And we have two exceptions. The format have two exceptions. The format have two exceptions. The format exception notice that it was it happened exception notice that it was it happened exception notice that it was it happened in at get age in at get age in at get age line 15 line 15 line 15 and that's the the bottommost entry and that's the the bottommost entry and that's the the bottommost entry means that's where the caller was that means that's where the caller was that means that's where the caller was that caught this issue. It's already caught caught this issue. It's already caught caught this issue. It's already caught the issue. But then down here we have the issue. But then down here we have the issue. But then down here we have another format exception. And notice another format exception. And notice another format exception. And notice this. We caught this at program.csline this. We caught this at program.csline this. We caught this at program.csline CS line 8 and it called line 15 and then CS line 8 and it called line 15 and then CS line 8 and it called line 15 and then it called in parse 32 and so on. So you it called in parse 32 and so on. So you it called in parse 32 and so on. So you have two different exceptions we caught have two different exceptions we caught have two different exceptions we caught with two different stack traces because with two different stack traces because with two different stack traces because they happen at different points. Okay.

  14. they happen at different points. Okay. they happen at different points. Okay. So this first one up here that happened So this first one up here that happened So this first one up here that happened because we caught it at the place where because we caught it at the place where because we caught it at the place where in our code the exception happened but in our code the exception happened but in our code the exception happened but then we threw the error again and we then we threw the error again and we then we threw the error again and we caught it again one level higher and now caught it again one level higher and now caught it again one level higher and now we have the entry or our stack trace we have the entry or our stack trace we have the entry or our stack trace that has all the callers because that's that has all the callers because that's that has all the callers because that's where the original call came from. So where the original call came from. So where the original call came from. So why would you catch and throw and catch why would you catch and throw and catch why would you catch and throw and catch and throw? Well, you might want to do and throw? Well, you might want to do and throw? Well, you might want to do some work in my method. So, the get age some work in my method. So, the get age some work in my method. So, the get age method, you might want to do some work method, you might want to do some work method, you might want to do some work here. Maybe what you do is you, you here. Maybe what you do is you, you here. Maybe what you do is you, you know, you you change a value or reset know, you you change a value or reset know, you you change a value or reset the value to null or some other way to the value to null or some other way to the value to null or some other way to say, hey, this is not good data. So, you say, hey, this is not good data. So, you say, hey, this is not good data. So, you might want to do something here. But might want to do something here. But might want to do something here. But then you might al this method right here then you might al this method right here then you might al this method right here might mean a class library. Class might mean a class library. Class might mean a class library. Class libraries should not talk to the user in libraries should not talk to the user in libraries should not talk to the user in most cases. There are slight exceptions, most cases. There are slight exceptions, most cases. There are slight exceptions, but for the most part, class libraries but for the most part, class libraries but for the most part, class libraries should even know about how to talk to should even know about how to talk to should even know about how to talk to the user. So, how do you tell a user the user. So, how do you tell a user the user. So, how do you tell a user there's a problem? Well, you do that by there's a problem? Well, you do that by there's a problem? Well, you do that by throwing it and then catching it at the throwing it and then catching it at the throwing it and then catching it at the user interface. The program.cs is our user interface. The program.cs is our user interface. The program.cs is our user interface. And so, you catch it user interface. And so, you catch it user interface. And so, you catch it here and say, "Hey, user, this is what here and say, "Hey, user, this is what here and say, "Hey, user, this is what happened." So, that way you can do happened." So, that way you can do happened." So, that way you can do different things. Over here, you can different things. Over here, you can different things. Over here, you can change data as necessary. And over here change data as necessary. And over here change data as necessary. And over here you can tell the user. So that's why we you can tell the user. So that's why we you can tell the user. So that's why we catch we throw. Now you may see catch we throw. Now you may see catch we throw. Now you may see something. I'm going to do something something. I'm going to do something something. I'm going to do something that is bad. Do not do this.

  15. that is bad. Do not do this. that is bad. Do not do this. Throw ex. Throw ex. Throw ex. Okay. And in fact right here um it gives Okay. And in fact right here um it gives Okay. And in fact right here um it gives us a warning saying rethrowing call us a warning saying rethrowing call us a warning saying rethrowing call exceptions changes the stack exceptions changes the stack exceptions changes the stack information. We're gonna do it for demo information. We're gonna do it for demo information. We're gonna do it for demo purposes. So let's even make it clearer purposes. So let's even make it clearer purposes. So let's even make it clearer here. Um, we're taking out the we're here. Um, we're taking out the we're here. Um, we're taking out the we're have just one exception caught then and have just one exception caught then and have just one exception caught then and it's in program.cs. Let's see how this it's in program.cs. Let's see how this it's in program.cs. Let's see how this looks. Okay, here's our exception. looks. Okay, here's our exception. looks. Okay, here's our exception. What's our stack trace say? Our stack What's our stack trace say? Our stack What's our stack trace say? Our stack trace says that program.cs line 8 had an trace says that program.cs line 8 had an trace says that program.cs line 8 had an exception. Cool. And then the exception, exception. Cool. And then the exception, exception. Cool. And then the exception, the next stack up is the place where the the next stack up is the place where the the next stack up is the place where the exception happened. And that is my exception happened. And that is my exception happened. And that is my method CS line 20. Well, that's not method CS line 20. Well, that's not method CS line 20. Well, that's not technically where the original exception technically where the original exception technically where the original exception happened. That's where we caught and happened. That's where we caught and happened. That's where we caught and rethrew it. So why is it that our stack rethrew it. So why is it that our stack rethrew it. So why is it that our stack trace only has these two entries and not trace only has these two entries and not trace only has these two entries and not the int parse int32.parse the int parse int32.parse the int parse int32.parse and it's its method? Well, because the and it's its method? Well, because the and it's its method? Well, because the actual exception is a new exception. We actual exception is a new exception. We actual exception is a new exception. We reset the stack trace. So the originator reset the stack trace. So the originator reset the stack trace. So the originator of the exception now becomes my method of the exception now becomes my method of the exception now becomes my method line 20. This originates a new line 20. This originates a new line 20. This originates a new exception. We do not want to do this. We exception. We do not want to do this. We exception. We do not want to do this. We want to just say throw. And the reason want to just say throw. And the reason want to just say throw. And the reason why is because this is the same why is because this is the same why is because this is the same exception,

  16. exception, exception, but it allows it to keep bubbling up, but it allows it to keep bubbling up, but it allows it to keep bubbling up, which means it catches even more of that which means it catches even more of that which means it catches even more of that stack trace. So now if we run this and stack trace. So now if we run this and stack trace. So now if we run this and we look at our stack trace, notice our we look at our stack trace, notice our we look at our stack trace, notice our stack trace. Yes, it's on line 8. And stack trace. Yes, it's on line 8. And stack trace. Yes, it's on line 8. And yes, it called my method line 15. But yes, it called my method line 15. But yes, it called my method line 15. But that method, that line called int that method, that line called int that method, that line called int system.int32.parse system.int32.parse system.int32.parse and that called the system.number.throw and that called the system.number.throw and that called the system.number.throw format exception. So we can see the full format exception. So we can see the full format exception. So we can see the full stack trace and understand the entirety stack trace and understand the entirety stack trace and understand the entirety of what our code was going through of what our code was going through of what our code was going through before it hit the problem. So stack before it hit the problem. So stack before it hit the problem. So stack traces are important and keep preserving traces are important and keep preserving traces are important and keep preserving that stack trace is important. Now there that stack trace is important. Now there that stack trace is important. Now there is something maybe call let's call this is something maybe call let's call this is something maybe call let's call this an exception to the rule and that is an exception to the rule and that is an exception to the rule and that is that you don't want to give this to the that you don't want to give this to the that you don't want to give this to the user. So this is really an unacceptable user. So this is really an unacceptable user. So this is really an unacceptable thing to do. console.rightline ex don't thing to do. console.rightline ex don't thing to do. console.rightline ex don't do that because the user first of all do that because the user first of all do that because the user first of all this is information they don't need.

  17. this is information they don't need. this is information they don't need. Second of all, this is information that Second of all, this is information that Second of all, this is information that tells the user how your application tells the user how your application tells the user how your application works. [snorts] Now the user knows, oh, works. [snorts] Now the user knows, oh, works. [snorts] Now the user knows, oh, there's a program CS line 8 and that's there's a program CS line 8 and that's there's a program CS line 8 and that's located here and there's a my method CS located here and there's a my method CS located here and there's a my method CS line 15 and so on up the stack trace. line 15 and so on up the stack trace. line 15 and so on up the stack trace. They can see how your application works, They can see how your application works, They can see how your application works, which gives them more information about which gives them more information about which gives them more information about how to exploit your application. how to exploit your application. how to exploit your application. So we do not give this to the user. So we do not give this to the user. So we do not give this to the user. However, this is very good information However, this is very good information However, this is very good information when debugging. You put this in your when debugging. You put this in your when debugging. You put this in your logs because your logs should be secured logs because your logs should be secured logs because your logs should be secured to only people who have access to them, to only people who have access to them, to only people who have access to them, who can read this programming sensitive who can read this programming sensitive who can read this programming sensitive data. But in here, you can look at this data. But in here, you can look at this data. But in here, you can look at this and go immediately I know where the and go immediately I know where the and go immediately I know where the exception occurred, what it was calling, exception occurred, what it was calling, exception occurred, what it was calling, what that was calling, and so on. So I what that was calling, and so on. So I what that was calling, and so on. So I know the path immediately to go to to know the path immediately to go to to know the path immediately to go to to find the issue to then track the issue find the issue to then track the issue find the issue to then track the issue through my code. So this is very through my code. So this is very through my code. So this is very important but it's not something the important but it's not something the important but it's not something the user should see. Now that is why we have user should see. Now that is why we have user should see. Now that is why we have ex message which is just this that can ex message which is just this that can ex message which is just this that can be okay. That could be you know good be okay. That could be you know good be okay. That could be you know good enough for your users maybe that does enough for your users maybe that does enough for your users maybe that does give you some information. The input give you some information. The input give you some information. The input string test was not in the correct string test was not in the correct string test was not in the correct format. um that could be okay. But more format. um that could be okay. But more format. um that could be okay. But more often what you want to see is where you often what you want to see is where you often what you want to see is where you do work

  18. do work do work where in here you say where in here you say where in here you say instead of ex you say something like um instead of ex you say something like um instead of ex you say something like um you you you did not give us a proper did not give us a proper did not give us a proper uh age, something like that. And maybe uh age, something like that. And maybe uh age, something like that. And maybe even put the the age they gave us. But even put the the age they gave us. But even put the the age they gave us. But that we can say, hey, this is what that we can say, hey, this is what that we can say, hey, this is what happened. And so if we run this, you happened. And so if we run this, you happened. And so if we run this, you would not give us a proper age. Okay, would not give us a proper age. Okay, would not give us a proper age. Okay, that's a little more clear. You want to that's a little more clear. You want to that's a little more clear. You want to say like this is an exception or an say like this is an exception or an say like this is an exception or an error. Say an error, probably not error. Say an error, probably not error. Say an error, probably not exception. Um and and maybe stop the exception. Um and and maybe stop the exception. Um and and maybe stop the application at that point. So application at that point. So application at that point. So that's how you would rewrite that and that's how you would rewrite that and that's how you would rewrite that and you would do something like you know uh you would do something like you know uh you would do something like you know uh you know log.log you know log.log you know log.log information information information actually log error. actually log error. actually log error. And this is what's nice. We do log And this is what's nice. We do log And this is what's nice. We do log error. You can say ex and then um this error. You can say ex and then um this error. You can say ex and then um this is my error info. Like you can put maybe is my error info. Like you can put maybe is my error info. Like you can put maybe in this text right here in this text right here in this text right here in here, but you're also passing in the in here, but you're also passing in the in here, but you're also passing in the full exception. Why the full exception?

  19. full exception. Why the full exception? full exception. Why the full exception? Because you want to know what type of Because you want to know what type of Because you want to know what type of exception was it handled, where was it exception was it handled, where was it exception was it handled, where was it handled, and what is that stack trace. handled, and what is that stack trace. handled, and what is that stack trace. Those are all important pieces of Those are all important pieces of Those are all important pieces of information. So the the ex has all of information. So the the ex has all of information. So the the ex has all of that. If you look at ex dot, you have a that. If you look at ex dot, you have a that. If you look at ex dot, you have a whole bunch of information in here that whole bunch of information in here that whole bunch of information in here that you want to make sure you capture all of you want to make sure you capture all of you want to make sure you capture all of that, which is why you log the whole that, which is why you log the whole that, which is why you log the whole exception to your logging system. exception to your logging system. exception to your logging system. Okay. So, by the way, I'm commenting Okay. So, by the way, I'm commenting Okay. So, by the way, I'm commenting this out because I don't have a logger this out because I don't have a logger this out because I don't have a logger in this console app. Um, that might be in this console app. Um, that might be in this console app. Um, that might be something else I do at some point is I something else I do at some point is I something else I do at some point is I believe in my in my perfect console demo believe in my in my perfect console demo believe in my in my perfect console demo app. Um, while it's a a tongue-in-cheek app. Um, while it's a a tongue-in-cheek app. Um, while it's a a tongue-in-cheek video showing you that writing hello video showing you that writing hello video showing you that writing hello world shouldn't be that complicated, um, world shouldn't be that complicated, um, world shouldn't be that complicated, um, and making decisions about what to use and making decisions about what to use and making decisions about what to use when, it does show you how to use when, it does show you how to use when, it does show you how to use dependency injection in a console app. dependency injection in a console app. dependency injection in a console app. So, that might be something that's So, that might be something that's So, that might be something that's interester interester interester here and log this to Sarah log or here and log this to Sarah log or here and log this to Sarah log or something else. Okay, so we have try, we something else. Okay, so we have try, we something else. Okay, so we have try, we have catch, we see that we can catch have catch, we see that we can catch have catch, we see that we can catch something that's already been caught something that's already been caught something that's already been caught somewhere else and rethrown. Um, all somewhere else and rethrown. Um, all somewhere else and rethrown. Um, all important information. But now let's important information. But now let's important information. But now let's talk about a couple other things. First talk about a couple other things. First talk about a couple other things. First of all, I'm saying you not give us of all, I'm saying you not give us of all, I'm saying you not give us proper age, but what if there's a proper age, but what if there's a proper age, but what if there's a different exception, right? So what if different exception, right? So what if different exception, right? So what if uh let's just do a throw um throw new uh uh let's just do a throw um throw new uh uh let's just do a throw um throw new uh exception that says um exception that says um exception that says um the system

  20. the system the system failed to boot up, failed to boot up, failed to boot up, right? Totally different exception right? Totally different exception right? Totally different exception message. I just created this by the way. message. I just created this by the way. message. I just created this by the way. It's just, you know, a generic error. It's just, you know, a generic error. It's just, you know, a generic error. But But But if we if we were to do this, let's just if we if we were to do this, let's just if we if we were to do this, let's just run this. run this. run this. It says you not give us a proper age. It says you not give us a proper age. It says you not give us a proper age. Well, what if I did? Because that's a Well, what if I did? Because that's a Well, what if I did? Because that's a different exception. It's not about different exception. It's not about different exception. It's not about that. In fact, if we do a um a console that. In fact, if we do a um a console that. In fact, if we do a um a console right line, just say ex on here. And right line, just say ex on here. And right line, just say ex on here. And let's comment this out let's comment this out let's comment this out and run this. and run this. and run this. We can see that this is a We can see that this is a We can see that this is a system.exception. The system failed to system.exception. The system failed to system.exception. The system failed to boot up and where it happened line 8. boot up and where it happened line 8. boot up and where it happened line 8. There is no more in the stack trace There is no more in the stack trace There is no more in the stack trace because that's all there is. That's because that's all there is. That's because that's all there is. That's where the exception happened and where where the exception happened and where where the exception happened and where it was called. So all in one line. So it was called. So all in one line. So it was called. So all in one line. So that's a different exception though. that's a different exception though. that's a different exception though. Notice it's a system.exception, Notice it's a system.exception, Notice it's a system.exception, exception, not a system, I believe exception, not a system, I believe exception, not a system, I believe format exception of our of our issue format exception of our of our issue format exception of our of our issue here. So this is why you can't put that here. So this is why you can't put that here. So this is why you can't put that message in a generic ex. However, what message in a generic ex. However, what message in a generic ex. However, what you can do is do this catch and say you can do is do this catch and say you can do is do this catch and say uh format exception ex.

  21. uh format exception ex. uh format exception ex. And then in here we can put our log And then in here we can put our log And then in here we can put our log statement statement statement and say you did not give us a proper age and say you did not give us a proper age and say you did not give us a proper age and otherwise say um and otherwise say um and otherwise say um something unexpected happened. something unexpected happened. something unexpected happened. This is a very generic um don't just This is a very generic um don't just This is a very generic um don't just have this again we're eating the have this again we're eating the have this again we're eating the exception make make it stop. But if we exception make make it stop. But if we exception make make it stop. But if we run this, it says something unexpected run this, it says something unexpected run this, it says something unexpected happened because we went down to this happened because we went down to this happened because we went down to this exception ex. However, if I were to exception ex. However, if I were to exception ex. However, if I were to comment out that one and have this one comment out that one and have this one comment out that one and have this one throw, well, it's going to say you did throw, well, it's going to say you did throw, well, it's going to say you did not give us a proper H. Why? Because not give us a proper H. Why? Because not give us a proper H. Why? Because that was a format exception. And we can that was a format exception. And we can that was a format exception. And we can catch multiple exceptions and do catch multiple exceptions and do catch multiple exceptions and do different things based upon the types of different things based upon the types of different things based upon the types of exceptions. So that way if you're doing exceptions. So that way if you're doing exceptions. So that way if you're doing let's say a file picker and there might let's say a file picker and there might let's say a file picker and there might be exception that is about the file be exception that is about the file be exception that is about the file didn't exist or a different one that didn't exist or a different one that didn't exist or a different one that said the directory didn't exist or a said the directory didn't exist or a said the directory didn't exist or a different one that says that the file different one that says that the file different one that says that the file couldn't be opened like these are all couldn't be opened like these are all couldn't be opened like these are all different types of exceptions that you different types of exceptions that you different types of exceptions that you could catch differently and say okay could catch differently and say okay could catch differently and say okay this is what I want to do for this this is what I want to do for this this is what I want to do for this circumstance versus that circumstance.

  22. circumstance versus that circumstance. circumstance versus that circumstance. So this allows you to catch these So this allows you to catch these So this allows you to catch these different exceptions. And Microsoft is different exceptions. And Microsoft is different exceptions. And Microsoft is usually pretty good about saying when usually pretty good about saying when usually pretty good about saying when you mar mouse over parse it says you mar mouse over parse it says you mar mouse over parse it says argument null exception, format argument null exception, format argument null exception, format exception and overflow exception. So exception and overflow exception. So exception and overflow exception. So those are the three exceptions that you those are the three exceptions that you those are the three exceptions that you can expect out of this method. That's can expect out of this method. That's can expect out of this method. That's also a very good thing to do is to also a very good thing to do is to also a very good thing to do is to document which exceptions you might be document which exceptions you might be document which exceptions you might be creating. creating. creating. So this way we could catch all three of So this way we could catch all three of So this way we could catch all three of these because we know that these might these because we know that these might these because we know that these might be occurring and do things differently be occurring and do things differently be occurring and do things differently based upon which one we're doing. So based upon which one we're doing. So based upon which one we're doing. So that is another thing you can do in that is another thing you can do in that is another thing you can do in order to order to order to tune your exception handling. Now I do tune your exception handling. Now I do tune your exception handling. Now I do want to point out that try catch is not want to point out that try catch is not want to point out that try catch is not a free operation. It does add overhead a free operation. It does add overhead a free operation. It does add overhead to your application. That does not mean to your application. That does not mean to your application. That does not mean you should not use it. Use it when it's you should not use it. Use it when it's you should not use it. Use it when it's important. Use it in order to make sure important. Use it in order to make sure important. Use it in order to make sure that you protect yourself against that you protect yourself against that you protect yourself against issues. However, don't get into the issues. However, don't get into the issues. However, don't get into the habit then of saying, "Oh, I can just habit then of saying, "Oh, I can just habit then of saying, "Oh, I can just try catch everything and use exception try catch everything and use exception try catch everything and use exception handling as a way to like control my handling as a way to like control my handling as a way to like control my application." Don't do that. Don't do application." Don't do that. Don't do application." Don't do that. Don't do that. Use exceptions for exceptional that. Use exceptions for exceptional that. Use exceptions for exceptional circumstances. Things that are outside circumstances. Things that are outside circumstances. Things that are outside your control or things that you can't your control or things that you can't your control or things that you can't stop from happening. For example, if stop from happening. For example, if stop from happening. For example, if you're creating up this method right you're creating up this method right you're creating up this method right here where maybe it's in a class

  23. here where maybe it's in a class here where maybe it's in a class library, you can't stop the user from library, you can't stop the user from library, you can't stop the user from giving you a bad age string. You can't giving you a bad age string. You can't giving you a bad age string. You can't stop that. Which is why int.parse has stop that. Which is why int.parse has stop that. Which is why int.parse has these exceptions because they they can't these exceptions because they they can't these exceptions because they they can't stop it. stop it. stop it. you might give them a bad string. And so you might give them a bad string. And so you might give them a bad string. And so these are the three circumstances that these are the three circumstances that these are the three circumstances that they're going to throw an exception for they're going to throw an exception for they're going to throw an exception for because they don't have a way to because they don't have a way to because they don't have a way to interact with the user and say no, you interact with the user and say no, you interact with the user and say no, you need to give us a a good value except to need to give us a a good value except to need to give us a a good value except to throw an exception. But when you control throw an exception. But when you control throw an exception. But when you control the user interface, you do have a way the user interface, you do have a way the user interface, you do have a way where you could say, you know what, that where you could say, you know what, that where you could say, you know what, that needs to be done. We need to do needs to be done. We need to do needs to be done. We need to do something different for this. And that's something different for this. And that's something different for this. And that's where you could just catch it here and where you could just catch it here and where you could just catch it here and do something different or even at the do something different or even at the do something different or even at the front end. But front end. But front end. But just don't use it for, you know, just just don't use it for, you know, just just don't use it for, you know, just normal usage of your application. Make normal usage of your application. Make normal usage of your application. Make sure it's for exceptional circumstances sure it's for exceptional circumstances sure it's for exceptional circumstances that you can't control. that you can't control. that you can't control. Okay. Um, couple other things here. Okay. Um, couple other things here. Okay. Um, couple other things here. Finally.

  24. Finally. Finally. So finally is going to run regardless of So finally is going to run regardless of So finally is going to run regardless of whether we have an exception in our try whether we have an exception in our try whether we have an exception in our try or not. So let's just say console right or not. So let's just say console right or not. So let's just say console right line the finally ran. Okay. So if we line the finally ran. Okay. So if we line the finally ran. Okay. So if we were to run this were to run this were to run this you would not give us a proper age. The you would not give us a proper age. The you would not give us a proper age. The finally ran your age is zero. Why is finally ran your age is zero. Why is finally ran your age is zero. Why is this important? And let's by the way, this important? And let's by the way, this important? And let's by the way, let's do um an actual value here. let's do um an actual value here. let's do um an actual value here. So it's 1 2 3 now. And we run this So it's 1 2 3 now. And we run this So it's 1 2 3 now. And we run this again. The finally ran. Your age is 1 2 again. The finally ran. Your age is 1 2 again. The finally ran. Your age is 1 2 3. So the age is correct. Now there's no 3. So the age is correct. Now there's no 3. So the age is correct. Now there's no exception exception that happened. But exception exception that happened. But exception exception that happened. But the finally still ran. Why is this the finally still ran. Why is this the finally still ran. Why is this important? Let's put it back test. Well, important? Let's put it back test. Well, important? Let's put it back test. Well, imagine you are talking to a database imagine you are talking to a database imagine you are talking to a database and you open a connection to the and you open a connection to the and you open a connection to the database. you try to do some work but database. you try to do some work but database. you try to do some work but the database doesn't exist and it the database doesn't exist and it the database doesn't exist and it crashes the the connection. Well, that crashes the the connection. Well, that crashes the the connection. Well, that would be an exception that you handle would be an exception that you handle would be an exception that you handle but you might need to close down that but you might need to close down that but you might need to close down that connection or release resources which is connection or release resources which is connection or release resources which is where you would do something in the where you would do something in the where you would do something in the finally.

  25. finally. finally. So that is something you need to think So that is something you need to think So that is something you need to think about is if I have unmanaged resources about is if I have unmanaged resources about is if I have unmanaged resources maybe open text file or or something maybe open text file or or something maybe open text file or or something else if something were to happen we have else if something were to happen we have else if something were to happen we have a crash do I properly close things down a crash do I properly close things down a crash do I properly close things down now that it's a little clearer here so now that it's a little clearer here so now that it's a little clearer here so you [clears throat] may have seen using you [clears throat] may have seen using you [clears throat] may have seen using uh ISQL data access uh ISQL data access uh ISQL data access SQL equals new SQL data access. SQL equals new SQL data access. SQL equals new SQL data access. This is just pseudo code by the way. This is just pseudo code by the way. This is just pseudo code by the way. It's kind of real, but it's not. So, you It's kind of real, but it's not. So, you It's kind of real, but it's not. So, you may have seen this where then we have a may have seen this where then we have a may have seen this where then we have a scope that you can do work inside these scope that you can do work inside these scope that you can do work inside these curly braces and then when you hit this curly braces and then when you hit this curly braces and then when you hit this curly brace right here, you are sure to curly brace right here, you are sure to curly brace right here, you are sure to close down your data access connection close down your data access connection close down your data access connection no matter what. Well, this actually uses no matter what. Well, this actually uses no matter what. Well, this actually uses try finally, not try catch, but try try finally, not try catch, but try try finally, not try catch, but try finally. So finally. So finally. So in the code behind like in the actual in the code behind like in the actual in the code behind like in the actual intermediate language, what it does when intermediate language, what it does when intermediate language, what it does when it converts this over to code, what it it converts this over to code, what it it converts this over to code, what it actually looks like actually looks like actually looks like is something like this, but instead of is something like this, but instead of is something like this, but instead of catch, we just have finally. [snorts] catch, we just have finally. [snorts] catch, we just have finally. [snorts] And in the finally this is where it c it And in the finally this is where it c it And in the finally this is where it c it does the dispose method. So it's you does the dispose method. So it's you does the dispose method. So it's you know uh SQL.ispose

  26. something like that where you have something like that where you have this code right here in here. this code right here in here. this code right here in here. Again this is all pseudo code. Again this is all pseudo code. Again this is all pseudo code. So, this is what's going on behind the So, this is what's going on behind the So, this is what's going on behind the scenes for our using statement is it scenes for our using statement is it scenes for our using statement is it actually does a try and a finally. No actually does a try and a finally. No actually does a try and a finally. No catch, just a finally. Why no catch? catch, just a finally. Why no catch? catch, just a finally. Why no catch? Because it doesn't want to eat the Because it doesn't want to eat the Because it doesn't want to eat the exception. It wants to let that pass on exception. It wants to let that pass on exception. It wants to let that pass on up. That's something that you want to up. That's something that you want to up. That's something that you want to do. You want to pass the exception on if do. You want to pass the exception on if do. You want to pass the exception on if you're not going to do any work on it. you're not going to do any work on it. you're not going to do any work on it. So, here we're kind of not doing any So, here we're kind of not doing any So, here we're kind of not doing any work on our exception. We shouldn't work on our exception. We shouldn't work on our exception. We shouldn't catch. We should just have output equals catch. We should just have output equals catch. We should just have output equals int.parse. parse. There's no reason to int.parse. parse. There's no reason to int.parse. parse. There's no reason to do a try catch here. Do not do any work. do a try catch here. Do not do any work. do a try catch here. Do not do any work. We're going to come back to that. We're going to come back to that. We're going to come back to that. But that's what we can do here is just a But that's what we can do here is just a But that's what we can do here is just a try finally and say, "Hey, try to do try finally and say, "Hey, try to do try finally and say, "Hey, try to do this code." And if there's an exception, this code." And if there's an exception, this code." And if there's an exception, doesn't matter. We still have finally at doesn't matter. We still have finally at doesn't matter. We still have finally at the end that does dispose no matter the end that does dispose no matter the end that does dispose no matter what. So that's what's going on behind what. So that's what's going on behind what. So that's what's going on behind the scenes with a using statement. Now, the scenes with a using statement. Now, the scenes with a using statement. Now, I mentioned that we should be doing work I mentioned that we should be doing work I mentioned that we should be doing work in my method. I want to show off one in my method. I want to show off one in my method. I want to show off one more thing we can do that kind of more thing we can do that kind of more thing we can do that kind of I show I said don't do this right but I show I said don't do this right but I show I said don't do this right but there is a case for having code where there is a case for having code where there is a case for having code where you create a new exception you create a new exception you create a new exception and that be may be maybe you have this and that be may be maybe you have this and that be may be maybe you have this code in your library and you're saying code in your library and you're saying code in your library and you're saying you know what this this message that it you know what this this message that it you know what this this message that it gives back isn't very clear so I want to gives back isn't very clear so I want to gives back isn't very clear so I want to do something better for the user well I

  27. do something better for the user well I do something better for the user well I could say throw new and let's just do a could say throw new and let's just do a could say throw new and let's just do a uh a format exception again and we're uh a format exception again and we're uh a format exception again and we're going to say format exception we can going to say format exception we can going to say format exception we can give a new message um give a new message um give a new message um the uh age of let's make this string the uh age of let's make this string the uh age of let's make this string interpolation here interpolation here interpolation here and we'll put in single single quotes and we'll put in single single quotes and we'll put in single single quotes and we'll say um age string is not a valid number. Okay, that's a is not a valid number. Okay, that's a clearer message we can show the user. clearer message we can show the user. clearer message we can show the user. But remember I said we don't want to But remember I said we don't want to But remember I said we don't want to lose the fact that our entire stack lose the fact that our entire stack lose the fact that our entire stack trace. What we're going to do is put trace. What we're going to do is put trace. What we're going to do is put comma and then say ex which is the inner comma and then say ex which is the inner comma and then say ex which is the inner exception. exception. exception. So you may say wait inner exception. So you may say wait inner exception. So you may say wait inner exception. Yes, because this exception is going to Yes, because this exception is going to Yes, because this exception is going to [clears throat] happen at this point on [clears throat] happen at this point on [clears throat] happen at this point on line 21. That's where the exception line 21. That's where the exception line 21. That's where the exception happens. So there's no more stack trace. happens. So there's no more stack trace. happens. So there's no more stack trace. So when we see in program.cs, Yes, we So when we see in program.cs, Yes, we So when we see in program.cs, Yes, we see the the console right line right see the the console right line right see the the console right line right here.

  28. here. here. Well, that's going to well, actually, Well, that's going to well, actually, Well, that's going to well, actually, let's do this. Let's go back and um put let's do this. Let's go back and um put let's do this. Let's go back and um put the console right line of ex. the console right line of ex. the console right line of ex. When we see that, it's going to say, When we see that, it's going to say, When we see that, it's going to say, well, the exception was caught on line well, the exception was caught on line well, the exception was caught on line 14 of program.cs and it occurred on line 14 of program.cs and it occurred on line 14 of program.cs and it occurred on line 21 of of this file. So that's not great. 21 of of this file. So that's not great. 21 of of this file. So that's not great. But we passed in the exception and so we But we passed in the exception and so we But we passed in the exception and so we can say oh there's an inner exception. can say oh there's an inner exception. can say oh there's an inner exception. So let's see this first in action and it So let's see this first in action and it So let's see this first in action and it says the format exception age or test is says the format exception age or test is says the format exception age or test is not a valid number and the format not a valid number and the format not a valid number and the format exception is not in the correct format. exception is not in the correct format. exception is not in the correct format. So we can show this to the user. But So we can show this to the user. But So we can show this to the user. But then we have our um our inner exception then we have our um our inner exception then we have our um our inner exception and our outer exception. So our outer and our outer exception. So our outer and our outer exception. So our outer exception right here. Remember I said on exception right here. Remember I said on exception right here. Remember I said on line uh nine and then on line 21 that's line uh nine and then on line 21 that's line uh nine and then on line 21 that's the exception occurred but the inner the exception occurred but the inner the exception occurred but the inner exception says well yes but on line 15 exception says well yes but on line 15 exception says well yes but on line 15 is where the actual exception happened is where the actual exception happened is where the actual exception happened in my case the in the inner exception in my case the in the inner exception in my case the in the inner exception and that was calling parse and that was and that was calling parse and that was and that was calling parse and that was calling this. So you don't lose the calling this. So you don't lose the calling this. So you don't lose the thread as long as you look at the inner thread as long as you look at the inner thread as long as you look at the inner exception.

  29. exception. exception. Why is this valuable? Well, let's do Why is this valuable? Well, let's do Why is this valuable? Well, let's do this. Let's do ex dossage. This is the this. Let's do ex dossage. This is the this. Let's do ex dossage. This is the thing we're going to show the user. So, thing we're going to show the user. So, thing we're going to show the user. So, if we do that, the user sees the age of if we do that, the user sees the age of if we do that, the user sees the age of test is not a valid number. That's more test is not a valid number. That's more test is not a valid number. That's more in line with what a user would actually in line with what a user would actually in line with what a user would actually want to see because they can understand want to see because they can understand want to see because they can understand that we have changed the message to be that we have changed the message to be that we have changed the message to be something they understand even though we something they understand even though we something they understand even though we didn't control the user interface. My didn't control the user interface. My didn't control the user interface. My method that's in a a class library. So method that's in a a class library. So method that's in a a class library. So we don't control the user interface, but we don't control the user interface, but we don't control the user interface, but we can do is say, hey, let's throw a new we can do is say, hey, let's throw a new we can do is say, hey, let's throw a new exception with a much better error exception with a much better error exception with a much better error message. We can show the user, but still message. We can show the user, but still message. We can show the user, but still pass along that inner exception that is pass along that inner exception that is pass along that inner exception that is the kind of the real path. And quite the kind of the real path. And quite the kind of the real path. And quite frankly, you can have multiple levels of frankly, you can have multiple levels of frankly, you can have multiple levels of these inner exceptions in certain cases. these inner exceptions in certain cases. these inner exceptions in certain cases. But what this allows us to do is to have But what this allows us to do is to have But what this allows us to do is to have good messaging going to the user while good messaging going to the user while good messaging going to the user while at the same time preserving that very at the same time preserving that very at the same time preserving that very very important stack trace. So there's a very important stack trace. So there's a very important stack trace. So there's a lot more to try catch than just saying lot more to try catch than just saying lot more to try catch than just saying try catch. There's multiple catches.

  30. try catch. There's multiple catches. try catch. There's multiple catches. There is catching and throwing again. There is catching and throwing again. There is catching and throwing again. There's you know create a new exception There's you know create a new exception There's you know create a new exception and having an inner exception. There is and having an inner exception. There is and having an inner exception. There is finally to make sure that we operate finally to make sure that we operate finally to make sure that we operate something or do something to clean up something or do something to clean up something or do something to clean up especially unmanaged code. There's a lot especially unmanaged code. There's a lot especially unmanaged code. There's a lot to do when it comes to exception to do when it comes to exception to do when it comes to exception handling. handling. handling. Don't just catch and eat errors. Don't Don't just catch and eat errors. Don't Don't just catch and eat errors. Don't just don't allow your application to just don't allow your application to just don't allow your application to continue in a bad state. Say, you know continue in a bad state. Say, you know continue in a bad state. Say, you know what? No, no. We need to stop and and what? No, no. We need to stop and and what? No, no. We need to stop and and stop right here or we need to go back stop right here or we need to go back stop right here or we need to go back and try again or something else to say and try again or something else to say and try again or something else to say no. This is the line. We cannot allow no. This is the line. We cannot allow no. This is the line. We cannot allow you to continue until you give us good you to continue until you give us good you to continue until you give us good information. So, make sure that you information. So, make sure that you information. So, make sure that you don't just eat errors and keep going. don't just eat errors and keep going. don't just eat errors and keep going. You make sure that you handle them You make sure that you handle them You make sure that you handle them properly. Tell the user what they need properly. Tell the user what they need properly. Tell the user what they need to know. Log what you need to, to know. Log what you need to, to know. Log what you need to, especially that stack trace information. especially that stack trace information. especially that stack trace information. But then don't keep going unless you can But then don't keep going unless you can But then don't keep going unless you can keep going in a proper state for your keep going in a proper state for your keep going in a proper state for your application. Okay, so that's exception application. Okay, so that's exception application. Okay, so that's exception handling on C#. There's lots to dive handling on C#. There's lots to dive handling on C#. There's lots to dive into here and you know there's more into here and you know there's more into here and you know there's more about the fact that you know try catch about the fact that you know try catch about the fact that you know try catch isn't free but it isn't something you isn't free but it isn't something you isn't free but it isn't something you should avoid as well. a lot more to should avoid as well. a lot more to should avoid as well. a lot more to understand, but this gives you a good understand, but this gives you a good understand, but this gives you a good overview of really how to use it well overview of really how to use it well overview of really how to use it well and make sure that you're properly and make sure that you're properly and make sure that you're properly handling different exceptional handling different exceptional handling different exceptional circumstances in your application.

  31. circumstances in your application. circumstances in your application. Thanks for watching and as always, I am Thanks for watching and as always, I am Thanks for watching and as always, I am Tim Corey.

Summary

This transcript discusses C# exception handling beyond just `try-catch` blocks, emphasizing the importance of reading stack traces, understanding exception types, and maintaining application health. The practical takeaway is that proper exception handling, including understanding their different components and when to control them, is crucial for robust application development, with resources for further learning available at imtimcorey.com.

View original episode ↗