Part 41 C# Tutorial Inner Exceptions in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Oh welcome to presume technologies I am venket this is part 41 in our exceptions in this session we will learn the basics of inner exceptions now in parts 40 exception handling we have seen that you know an exception is nothing but actually an instance is a class that is being blown when an unforeseen event occurs and to handle exceptions we use try catch and finally blocks and we know that and any exception object within nor that actually derives directly or indirectly from system called exception class and we know the base exception class exposes properties like message stack trace inner exception etc in the previous session we have seen what the message and stack stack trace property gives us in this session we will learn about inner exception okay this is a little bit tricky one so let's understand inner exceptions with an example what I want to do is I want to create a very simple program that can add dad that can divide two numbers so the program will prompt the user to enter the first number and it will prompt him to enter the second number and then once we have moved the numbers we will try to divide them and when we try to do that there are several exceptions that can occur so let's start with the program so first we'll ask the user to enter their first number console.writeline and first number so once the user enters the first number we have to read it from the console so to read it from the console we use console dot readline and we know that console dot readline returns the number in the form of a string so we need to convert that to an integer we use the convert class 20:30 to convert that to integer once we have converted that to integer store it in a variable for calculation purposes later so I am storing it in a variable called first number similarly from the user to enter second number and read that from the console convert that to interior and store it in a variable called second number and finally calculate the result so result is equal to first number by second number and what we want to do we want to print that back to the user it's a console or trade line result equals so we print the result now if you go ahead and run this program it will ask the user to enter first number he enters his first number a second number result is to press any key to continue now this program can fail for a variety of reasons you know for example if I run this program if I enter alphabets instead of numbers something like this obviously there is a format exception because that string cannot be converted to integer similarly this program can also fail if I enter a very big number you know obviously an integer variable can hold only a a number of specific size but this is large number an integer variable cannot hold that so it will overflow and we'll get an overflow exception and since we are dividing two numbers if you divide a number by 0 you know it's the result is infinity and obviously it's illegal in mathematics so that's why when we try to divide a number by 0 what happens we get a divide by exception so this program fails for a variety of reasons ok no matter which reason this program fails you know when it fails there is an exception so I want to be able to handle that exception and to do that we use try catch blocks so try catch now whatever is the exception that occurs I want to catch that not only catch that exception just give a message to the user saying that you know there is a problem please try later okay now after I catch the exception what should we be doing we should either be logging that exception or emailing the exception information to an administrator so he can look into the program and solve that ok but in our case let's go ahead and log that to your file now I have actually created a file here you know I have C Coulomb sample files directory and in this directory I have a log file log dot txt and if you look at that it's currently empty now if there is an exception I want to write that exception information to that log file and to do that I will make use of stream writer class and streamwriter class is present in system dot IO namespace because it's to do with the input and output from files so system dot IO in so let's use stream writer class so streamwriter SW is equal to new streamwriter and if you look at the constructor on the stream writer class there are several overloads available but we'll use the second overloaded version which takes in as a part of the file so what's the path of our file it's actually present string let's call this as file path the variable and what's the path it is C : sample files long dot txt so C colon backslash sample files back slash log dot txt and since backslash you know has got a specific meaning you need to use where bottom literal okay now we pass on the file path variable into the stream writer class and then what we need to do is okay the stream writer class will be in a position to write to that file okay now you need to tell to the string writer what you want to write I want to write the exception type let's say for example if it's a format exception I want to you know just try that to the file if it's a / 0 whatever is the exception type I want to write the typed information into that log file and to get the type of the exception you actually have a method called get type and we know that if we want to the message we can use the message property if we wanted Strax stacktrace we can use the stack trace property similarly if I want to find the type of the exception is it / 0 format exception overflow argument what is the type of the exception then you can use cap type method and then on that you can actually use the name property or you can also use the full name which will also give the fully qualified name including the namespace but let's say we want just a name and I want to write the type information the type name to the file so write that and then finally close the stream stream writer okay so so far so good there is an error SW is a variable but is used like streamwriter yep stream right or dot there is a method called right so we use the write method so we were using that object like you know a a method but that's an object all right so now if we go ahead and run this what's gonna happen if there is an exception the exception information will be returned to this particular file so let's see if it has actually doing that okay to enter enter first number let's say I enter a very big number obviously and India variable cannot hold that there will be an exception so when there is an exception what happens the catch block catches that exception writes the exception type information to this file and then it simply prints this message there is a problem please try later now if we go to our folder you should see overflow exception and if you want to write the stack trace and other information you can do that as well all all you need to do is just say SW dot write whatever you want write for example X dot message and similarly you can also do the same thing for stack trace so now if I run this and maybe I enter some alphabets obviously there it will fail and you come here and check so format exception input string was not in a correct format and obviously there is no space because we need to tell that you know probably you can just say yes w dot write line just to format that a little just write a line in that file we run this again and some alphabets it fails again so you go check that so now it comes cleanly in two different lines however we are here today to learn about inner exceptions okay all this well now you know there's an exception the file is there it it writes to that but what if this file is not there if the file is not there we will get a file not found exception okay so first I mean you know just to be safe we will check if the file exists or not if the file exists only then I want to write to that file otherwise I want this program to throw a file for exception so how do we do that again in system that IO class there is a file class and on this file class there is a static method exists and to this method you can actually pass in the file path and it will verify if the file is there it will return true if not it will return false so in the file at this part exists only then do this if the file is not there then through a new file not found exception so we are throwing a another exception here and you you can't give the meaningful message you want maybe you can just say this file path so if you look at that the constructor of file not found exception you know there is a message there is one overloaded version which takes the message so the message I want to say is okay a file at this part you know this variable has that part is not present I just want to show that message I mean through an exception with that message now if you look at the other overloaded version it's talking about an exception okay now what is this parameter this is nothing but okay look at this first of all why did we ever get into this cache block because there is an exception you know while trying to execute this code as a result of that exception we got into this block and we are writing that exception you know the original exception could be overflow exception okay so when that exception occurred me you know the control came into the cache block we are trying to write the exception information into this file but unfortunately we don't have that file there and we are throwing another I mean obviously if the file is not then we cannot write to it so we are throwing a file not found exception but I don't want to lose the original exception which landed me in this cache block okay so if you want retain that exception as well you pass that exception e^x in this case as a parameter to the of this exception so what will happen this is the you know this is the original exception and this is our current exception okay so now let's run this program and see what happens look at this an exception is being drawn from the catch block and by the way to throw an exception you use the true keyword we have not seen about this in the previous session intentionally because we want to talk about this here so if you want to intentionally do an exception you use the true keyword okay so we are throwing another exception and since we want to retain the original exception which landed us in this catch block we are passing that exception as a parameter to the constructor of the current exception all right now let's run this program and see what's gonna happen so when I run this ok enter your first number I'm gonna enter 10 enter your second number 0 so obviously we'll get it divided by zero exception which will take us to this cache block and then what happens it will okay that file is still there so it will not throw file not for an exception but anyhow let's see that so obviously what happened here it came here the file is there so the divide by zero a exception information will be returned to that log file so all the version I believe so if you come here and open that so divide by zero exception attempted divided by zero all right so how do I make this program fail so that you know we get we get this one so now let's say I will mark this as log one B or you can just change somebody will somebody has changed let's say the name of the file to log one now what happens when your program tries to write the exception information to that log file it will not find that file and there will be another exception thrown here okay so let's run this actually so end the first number 10 and the second number zero and what happens look at this now what happened it tried to divide the number by zero so obviously divided by zero a exception will occur it comes to the catch block and it tries to you know find this file it's not found so it comes to the else block it doesn't even execute this line and this cryptic message is shown to the end-user so end-users will not understand anything about this exception because it's definitely cryptic so what we want to do if you want to handle this exception then you want to wrap this entire code in another try block so what I want to do is put another try block so try this try that and there's another cache block so catch exception let's say we already used e^x here so we have to use something else let's call this exception ok now let's go ahead and put a breakpoint here and run our program in the debug mode okay let's put another breakpoint in this cache block so now we run the program so it's asking the user to enter first number 10 second number zero look at this the moment we do that we get a divided by zero exception attempted to divide by zero and if you look at this divided by zero exception okay now what does this cache block tries to do it tries to write that exception information into this file so let's see what happens okay that particular file and it checks ok does this file exist in C sample files do we have the log file now it's log 1 dot txt the file is not there so what happens it comes to the else block and what is that block going it is throwing another exception file not found exception ok obviously because that file is not found and then to this constructor we are passing the original exception as well so what happens this object will be thrown along with this wrapped inside the inner exception property of that or check so now what happens since this entire cool is in the outer try block the control comes to this cache block and if I press f10 here you can actually see you know look at this inner exception is there is something in the inner exception and if I expand that divided by zero so the original exception is divided by zero exception but what is the current exception file not found exception ok so here we get an inner exception now let's stop this ok let's print those messages in an exception and the current exception so console dot write line so if I want to get the current exception what we will do exception this is the exception object dot get type let's say I just want the name of the exception so that's our current exception and just to make a differentiation we will say current exception that's our current exception and similarly if we want the inner exception we will need to say this exception object so obviously this exception object along with the message and source property there is an inner exception from the as well so inner exception dot you know maybe cat type and then the name of that so we know that in this case in an exception is divided by 0 exception and we want to print that there okay so let's run this now ok in the first number 10 and the second number 0 it tries to write that for some reason the exception you know information is not being shown okay that's because we need to put that placeholder okay so let's run that now and the first number 10 and the second number so in an exception is I mean current exception is file on found in an exception is divided by 0 okay now let's say what happens if you don't pass this exception object as a parameter to this constructor let's see what happens if you don't pass the original the original exception object then the inner exception property of this exception object will be null and if you try to invoke any method or property on another object you will get null reference exception that is why you should always be checking if in that exception is not let me show you that so I run this now I under 10 under actually let's put that in debug mode so you can actually see that so let's run this and the first number 10 and the second numbers 0 so we get 2 divide by 0 exception I press f10 the file is not there we get violence on exception and we come here into the outer catch block look at here we are not passing the original exception so inner exception on this object will be null so if you look at inner exception it's actually now okay and now as this is now look at the intellisense as this object is now and I'm trying to get type method on mallow object we get a null reference exception so let's see that ok prints the first line actually if you look at the console you know we got the current exception but you know that inner exception is now if I move my cursor over the danger list and shows that and on the null object we are calling get type it will fail here and there is no and this cache block is not in any try block so the program will crash if I press a5 look at last object reference we get a null reference exception object reference not set to an instance an object okay so that's why anytime you try to retrieve the inner exception make sure you always check if that exception object dot inner exception not equal to now only then do this this way we can avoid that null reference exception so if we run this now enter your first number ten second number zero so it will only print the current exception since inner exception is now it will not print that okay but if you want that inner exception what you can do you can just pass in the original exception object to this constructor and that will give you both the inner exception and the outer exception so outer exception file and so on in an exception divided by zero I hope you have enjoyed this session so what is the inner exception property does it returns the exception instance that costs the current exception rule so the inner exception will actually give you the original exception which caused the current exception but for that to happen you need to pass the original exception as a parameter to the constructor of the current exception object to retain the original exception pass it as a parameter to the constructor which we have just spoken about always check if another exception is not now before accessing any property or method on the inner exception object otherwise you will get a null reference exception and to get the type of an inner exception you just use the get type method and remember the base exception class has all these properties message stack trace you know inner exception get type which are really valuable to find you know very useful information for debugging all right on this slide you can find resources for asp.net and C sharp interview questions that's it for today thank you for listening have a great day
Info
Channel: kudvenkat
Views: 186,354
Rating: undefined out of 5
Keywords: Inner, Exceptions, in, C#, c# inner exception example, c# check if inner exception is null, how to see inner exception visual studio, get inner exception message, c# get type of inner exception, c# inner exception null, visual studio inner exception, asp net inner exception, c# inner exception stack trace
Id: MO3sOTWfZPc
Channel Id: undefined
Length: 23min 22sec (1402 seconds)
Published: Thu Jun 28 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.