I'd like to ask this question (also here), but this time about C++.
What is the difference in C++ between
try { /*some code here*/} catch(MyException& ex) { throw ex;} //not just throw
and
try { /*some code here*/} catch(MyException& ex) { throw;} //not throw ex
Is it just in the stack trace (which in C++ is in any case not a standard as in C# or Java)?
(If it makes any difference, I use MSVS 2008.)
throw ex : If we use "throw ex" statement, stack trace of exception will be replaced with a stack trace starting at the re-throw point. It is used to intentionally hide stack trace information.
The basic difference is that the Throw exception overwrites the stack trace and this makes it hard to find the original code line number that has thrown the exception. Throw basically retains the stack information and adds to the stack information in the exception that it is thrown.
The throws keyword is used to declare which exceptions can be thrown from a method, while the throw keyword is used to explicitly throw an exception within a method or block of code. The throws keyword is used in a method signature and declares which exceptions can be thrown from a method.
throw rethrows the caught exception, retaining the stack trace, while throw new Exception loses some of the details of the caught exception. You would normally use throw by itself to log an exception without fully handling it at that point.
throw;
rethrows the same exception object it caught while throw ex;
throws a new exception. It does not make a difference other than the performance reasons of creating a new exception object. If you have a exception hierarchy where there some other exception classes derived from MyException
class and while throwing an exception you have done a throw DerivedClassException;
it can be caught by the catch(MyException&)
. Now if you modify this caught exception object and rethrow it using throw;
the type of exception object will still be DerivedClassException
. If you do throw Ex;
the object slicing occurs and the newly thrown exception will be of type MyException
.
[C++ FAQ Lite § 17.9] What does
throw;
(without an exception object after thethrow
keyword) mean? Where would I use it?You might see code that looks something like this:
class MyException { public: ... void addInfo(const std::string& info); ... }; void f() { try { ... } catch (MyException& e) { e.addInfo("f() failed"); throw; } }
In this example, the statement
throw;
means "re-throw the current exception." Here, a function caught an exception (by non-const reference), modified the exception (by adding information to it), and then re-threw the exception. This idiom can be used to implement a simple form of stack-trace, by adding appropriate catch clauses in the important functions of your program.Another re-throwing idiom is the "exception dispatcher":
void handleException() { try { throw; } catch (MyException& e) { ...code to handle MyException... } catch (YourException& e) { ...code to handle YourException... } } void f() { try { ...something that might throw... } catch (...) { handleException(); } }
This idiom allows a single function (
handleException()
) to be re-used to handle exceptions in a number of other functions.[C++ FAQ Lite § 17.11] When I throw this object, how many times will it be copied?
Depends. Might be "zero."
Objects that are thrown must have a publicly accessible copy-constructor. The compiler is allowed to generate code that copies the thrown object any number of times, including zero. However even if the compiler never actually copies the thrown object, it must make sure the exception class's copy constructor exists and is accessible.
(edited for more clarity on what I thought was obvious...)
catch(MyException& ex) { throw ex; }
may copy ex
, with all the issues that it entails; catch(MyException& ex) { throw; }
may not.
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