I am not sure if this is a issue with the compiler or if I am doing something wrong. I am using Visual Studio 2013 compiler.
I have a class where I need to acquire significant amount of resources in my constructor initializer list most of which can throw an exception. I wrapped up the member initializer list in a function try block and caught the exception there. But my program still aborts even though the catch clause doesn't re-throw the exception. I am not allowed to post the actual code. So I have reproduced the issue with this equivalent demo code. Can someone please help me address this?
#include <iostream> using namespace std; class A{ public: A() try : i{ 0 }{ throw 5; } catch (...){ cout << "Exception" << endl; } private: int i; }; int main(){ A obj; }
On executing this code I get a windows alert "abort() has been called". So I guess the system is treating this as an uncaught exception and calling terminate().
On the other hand if I wrap the construction of the object in main() in a try-catch block then the exception is caught properly and the program terminates normally.
Can someone please tell me if I am doing something wrong here?
The try statement allows you to define a block of code to be tested for errors while it is being executed. The throw keyword throws an exception when a problem is detected, which lets us create a custom error. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
The primary purpose of function-try-blocks is to respond to an exception thrown from the member initializer list in a constructor by logging and rethrowing, modifying the exception object and rethrowing, throwing a different exception instead, or terminating the program.
Interview Answers. Yes, we can write try catch and finally block in a constructor ant it works properly. No we can not. Constructor is only for initialization of fields.
Try/catch blocks should generally be within a function or method, you have it immediately after the public keyword. If you're throwing an exception from the constructor, you don't catch it in the constructor. Instead you catch it in the code that called the constructor ( main in this case).
There's a relevant gotw
http://gotw.ca/gotw/066.htm
Basically even if you don't throw in your catch block, the exception will automatically be rethrown
If the handler body contained the statement "throw;" then the catch block would obviously rethrow whatever exception A::A() or B::B() had emitted. What's less obvious, but clearly stated in the standard, is that if the catch block does not throw (either rethrow the original exception, or throw something new), and control reaches the end of the catch block of a constructor or destructor, then the original exception is automatically rethrown.
This is normal behaviour according to the cppreference.com documentation for function-try
blocks: a so-called function-try-block on a constructor or destructor must throw from its catch-clause or else there is an implicit rethrow after the catch-clause.
This makes perfect sense: the object A
has not been properly constructed and hence is not in a state fit for use: it must throw an exception. You have to ensure whether the construction succeeded at the place where the object is constructed, i.e. in the case of your example in main()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With