简体   繁体   中英

throwing exception from constructor

Out of my curiosity..

I notice when I throw exceptions from constructor, if I compiled the code in debug mode and if I click on continue debugging (or continue stepping through), it won't exit the constructor until it reaches the end. Please note I don't have try{}catch{} wrapping the code that instantiate MyClass object.

I tried this in release mode, and can't really tell if it exits the constructor after the first throw or the last throw. Do you know if in release mode it leaves ctor since the first throw or the last? And why does it let me go to the next line when I'm in debug? shouldn't throw just exit the scope it's in?

MyClass::MyClass()
{
   throw "exception1";
   throw "exception2";
   throw "exception3";
}

MyClass a;

I suspect that it's a debugging issue. An uncaught exception should, by default, kill the program. But your debugger stops the program at the line that caused the exception, instead. And the "continue debugging" button tells the debugger to just ignore the last fatal problem and continue going.

So the debugger continues on in the program until it reaches the second throw . Which would, again, be fatal. So the debugger stops there. Etc.

If there's a place that actually catches the thrown exception, you should see different behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM