Exception Handling in C++ Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] [Music] Yoga is ten matter for simple snippets and today in the C++ programming tutorial we will be talking about the exception handling concept in C++ now in the previous couple of videos we were discussing about the functions its applications and it's different types so if you have missed that video you can check those videos in this playlist itself so with that being said let's get started with exception handling so what exactly is an exception in a program so let me just read out the first line so an exception is an unexpected problem that arises during the execution of a program so it may happen that sometimes during programming you might have syntactically written the program correct which means that the compiler has compiled a program correctly however during its execution there might be some issues and some errors which come at the runtime and at that time the program crashes so these kind of issues are known as exceptions for example you create an array of size 5 but try to access the 6th or 7th index position of that array and at that time your program will crash all for example you are trying to open a file which does not exist so that could be another example so these kind of scenarios lead to crashing of the program in between so exception handling mechanism provides a way to transfer control from one part of the program where the error exists to another so this makes it easy to separate the error handling code from the code written to handle the actual functionality of the program which essentially means that even if there is an exception your program won't crash in between and you can handle that exception and provide certain code which takes care of that exception and C++ exception handling is built upon three keywords so these are the syntax or the keywords which are essential so they are try catch and through so let us understand more about these three keywords the first one is try block the reason why it is called a block is because it is enclosed in curly braces just like a function however it is not a function so a block of code which may cause an exception is typically placed inside this try block and it is followed by one or more cache blocks so if an exception of course it is thrown from the try block to the catch block so basically where ever you feel that the exception might occur you put that entire piece of code inside the try block now comes the catch block so this block catches the exception thrown from a try block code for handle the exception is written inside this catch block the code can be anything which handles the section for example just simply showing an error message or some code which will actually correct that exception and so on and so forth lastly we have the true syntax keyword so a program throws an exception and a problem shows up this is done using a two keyword now you can explicitly throw an exception for example you want to show a divided by 0 exception and we try to implement that practically as well so for a certain condition you want to throw an exception you use the throw keyword now that will be more clear when we actually go ahead and write a program lastly every try-catch should have a corresponding cache block that is every try block should have a corresponding cache block and a single try block can have multiple cache blocks now this was all about the theoretical aspect of exception handling and the tale different keywords which we'll be using to perform exception handling so this was about the theoretical aspect of exception handling in C++ so now let's try to create a program and implement this exception handling so quickly open up your day C++ ID or whichever ID you use and type out this program which are written over here so this is the main function from where the execution will begin so what we are going to do is we are going to write a program which will accept two numbers from our user insurance division so there is a case where in we take numerator and denominator and if the denominator is 0 the division should be not defined or infinite so in that case the program will crash if it is not in a try clash block and I will try to show you how it works so let me just create force to variables as a int numerator common denominator you see a message and ask the user to enter the numerator and denominator I will say enter two numbers I will say enter numerator and denominator then we will take input from the user I will see in result is equal to numerator divided by denominator and we will try to display this so I will say division is and I'll print the result Lee so let's save this as exception handling but CTP and let's try to compile and run this so there were zero errors and zero warning so we have written the program correctly but this is without the exception handling so right now what I'll do is I'll enter numerator 5 denominator 5 and you get the division is 1 because 5 divided by 5 will give you 1 correct now let's try to put the denominator is zero so when I say 5 as numerator and 0's denominator you can see the programmers stopped working because 5 divided by zero should give infinity or not defined as a result and this is the reason why the program crashes because it is not meant to calculate something which is not defined or infinity so this is the exception situation now since there was no try-catch block involved the program stopped working abruptly and it hanged so this situation is something which we need to avoid so now what I'm going to do is I'm going to modify this code and add try cache block to it so we know the exception will be happening at this line of code because this actual division happens here now according to the try definition we add the try block where we feel the exception is going to happen so as I mentioned we have to use the syntax as follows left to right try opening and closing curly braces and include that part of the code which you feel where exception might occur so I just wrote a try block and inside that we are performing the division now here we can check if denominator equal equal to zero then we need to throw an exception so this is how the syntax for throw is so you say through thr Oh W and we need to throw something that is some value so what we'll do is we just threw this denominator itself that is it is going to throw the value 0 and you don't need opening and closing curly braces is there is only single statement inside s block but for simplicity I will just write it over here as well so when this denominator is going to be zero it is being thrown out sir this entire dialogue and this division will not occur instead the controller will be transferred from your corresponding cache block so now you need to write the code for cache block so you catch so these are the impacts of cache block is opening and closing round brackets and inside this we have to catch something that is the value that is being taught own so in this case the value that is being tuned is an integer value so inside this L get into X which is short term for exception and then again opening and closing curly braces and inside this we can say on message saying divide by zero not allowed which is an exception so quick recap initially when we did not have try catch and we did not handle the exception our program was crashing because when we entered denominator is zero anything divided by zero gives us not defined or infinity which is not being handled by the program and the program was stopping abruptly and it was crashing so now what we've done is you've handled this exception by using a try block inside the try block we included the line where the actual division happens that is this line and before this division happens we check whether the denominator is zero or not and if the denominator is zero you throw that zero and the control is transferred right at this sentence itself or right at this line itself to the catch block so this result or this line will never happen or this line will never be calculated by the program itself so from here itself the transfer will be thrown to the cache block inside the cache we are catching that integer value that is being thrown which is essentially going to be zero so if you want you can print it over here itself and we print out the message saying exception divided by zero not allowed and if the denominator is not zero then this s block will not be executed the result will be calculated and you can see at the last line the division will be soon so let's save this and try to execute this and say compile and run ok we got an error because this result was declared and defined inside the try block it needs to be declared over your just remove this save this this is because the result variable was declared inside the try try block so when the try block goes over it scoreboard over that is it was not it went out of scope so in order to make it global that is globally available inside the entire intention you have to declare it over your let me just save this let's start to compile and then this okay so zero errors and zero warnings now it is asking for numerator and denominator so let's try to enter something and say six as your denominator is two the division is three so it's working fine when we enter proper integers that is denominator is not zero now let's try to compile and run it again I will say five or six and now I will say denominator is zero so there you go got an exception that is divided by zero not allowed and you can see this value is zero also the division is zero B is being printed because that is outside the cache block since the division did not occur the default value of this result variable is set to zero that is why we got the output as 0 also notice that the program did not crash in between or did not hand so we successfully handled the exception and we also showed an exception message so this is how you perform exception handling and it is essentially done so that your program does not crash in between and you execute the program successfully so that's it for this video guys I hope you understood the concept of exception handling why we need exception handling and how to practically implement exception handling now we can also create our own user-defined exceptions and define when exactly an exception should occur and when not but that we will discuss in later part because it would involve inheritance and classes which we still have to discuss so if you have any queries or comments you can always put them in the comment section and if you like this video give it a thumbs up share it with your friends and don't forget to subscribe to our Channel peace
Info
Channel: Simple Snippets
Views: 116,951
Rating: undefined out of 5
Keywords: exception handling in c++ in english, c++ exception handling mechanism, exception handling program example in c++, exception handling, eception handling program in c++, exception handling c++ tutorials, exception handling in c++, exceptions in c++, exception handling cpp, throw in c++, c++ excpetion handling concept with program example, exception handling in c++programming, exception handling in c++ tutorial, programming, simple snippets, exception, c++ tutorials for beginners
Id: EyXXLpFriMc
Channel Id: undefined
Length: 10min 11sec (611 seconds)
Published: Tue Jul 25 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.