Home
Username:
Password:
C++ Fundamentals Tutorials

Exception Handling / Stack Unwinding




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

This is Tim Heagarty with the Virtual Training Company C Plus Plus Fundamentals. We're finishing up our talk here on exception handling with the concept of stack unwinding. Remember I asked you what would happen if you were in a try block and a try block made a function call and maybe that function gets called into creates some class objects and their constructors get called and then we do some other work and call another function and then we throw down in the middle. Well that throw is going to cause us to have to undo all of the work that has occurred before the throw, we have to go through all of the destructors for all of the objects that were created in reverse order. Just the way we do when we are exiting the program, so we have to release all that memory, run all those destructors and then find our way to the catch blocks and see if we can get a catch block that will fit our throw. If there is not a catch block to fit we will call a function called unexpected. Unexpected simply does an abort, which dumps off the rest of the code and exits, exit out of the rest of the program. There is also a terminate, there's a terminate function that can be called if we try to catch an exception that we have not taken care of. We have not set up a catch for their particular kind of exception, so we can go and do a terminate. So let me show you some code here. Code is always simpler to talk about. So here we have, this is a new structure and it has a message in it and it has a constructor that will build this message so E can be used with a message in it and that's what we are going to be throwing is our error structure. Now here's a new function called my terminate, we want to replace the terminate function that's in as part of the, the runtime. So that we can see something happen and we will replace that with a little output called my terminate that we're there. Here's a simple structure A, with a constructor and a destructor and here's a structure B, with a constructor and a destructor and notice here that the destructor of A while its saying I'm here in destructor of A, it then throws another exception. So we get another exception while we are trying to get out of the original throw. That will cause the terminate to run. Alright so that's an exception thrown in the destructor, now we are going to see some very odd behavior which you certainly seen from programs before. But now your gonna know a little bit of what goes on. Here's our main function and we have try and we are going to call set, terminate. Set terminate tells the runtime to use this function my terminate as its termination function so that should the runtime have to call my terminate or has to call the built in terminate it will in fact call my terminate so we, we set that up first. Then we say that we're in our try block and we build A and B of our structs. Really just classes, A and B. And then we will throw an exception. So an exception is thrown in the try block of main and that's our pointer, that's a character pointer so that's going to match. Now, when that throw occurs we have to undo B and then we have to undo A, we have to destruct them so we will go up into the destructor of B and then because its in reverse order, we'll go into the destructor of A and we will throw another exception. And that's when everything is gonna break loose and you are gonna see what happens. We are gonna call my terminate but also windows is not going to like it. Here's our catches, we can catch our constant character pointer and that is our normal exception handler, here is everything else. Some other exception and here is resuming execution of main after the catch blocks. So let's run this what happens. See we were in the try block, and we got into the constructor of A, we got into the constructor of B, then we were suppose to undo everything. We did a throw, so we had to hit the destructor of B and then we got into the destructor of A and at that point we did another throw and that second throw did a call to my terminate. Well my terminate worked but it also calls into the operating system and tells it that we have requested a terminate in an unusual way. Windows does not let this go easily. So we can ignore this termination and we will still get our, our little press key to continue so and this information comes on through that the application has requested to terminate in an unusual way. Alright that's the kind of thing that happens, you can set your own termination but you have to be very aware in stack unwinding of the terminate function and the unexpected. The unexpected function is done the same way, set unexpected, and then some function name if you don't want the compilers unexpected to be called. And unexpected again is only used if you don't have a catch like this with the ellipse because that catch everything and of course unexpected would not be called because you expected everything, by including this. If you didn't use this catch with the ellipse in it and you only had specific catches, if you pass, I'm sorry, if you threw something unexpected there you go, you're gonna call the unexpected function and in the normal C Plus Plus runtime, unexpected just aborts the program, it throws away everything else and closes up shop and leaves and these files are hanging open and you know whatever else was going on so its not a clean way out. You want to at least try to have this catch ellipse if you're gonna be using try, catch and throw blocks. You want to have this one so that you can try to catch everything else and not wind up into that weird condition we had with the unexpected and also in the termination and remember you get to terminate if a destructor does a throw if it has a problem while its destructing, its entirely possible but you want to try to code your way around so that you do not try to throw an exception while your getting out of an exception.

Tutorial Information

Course: C++ Fundamentals
Author: Tim Heagarty
SKU: 33797
ISBN: 1-934743-09-7
Release Date: 2007-09-14
Duration: 4 hrs / 55 lessons
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available