Throwing exception exception caught by pointer

In C ++, what is the difference between the following examples?

Re-throw pointer :

catch (CException* ex)
{
    throw ex;
}

      

Simple re-throw :

catch (CException* ex)
{
    throw;
}

      

When the rethrow is caught, will the stack trace be different?

+1


a source to share


2 answers


Yes. Basically, you are throwing the object yourself in the first case. It looks like you threw the exception yourself on the line throw ex

. In the second case, you just let the original object go up in the call stack (and thus keep the original call stack), these are different. Usually you should use throw;

.



+7


a source


I think there is a performance difference. The second version will not create a temporary copy of the exception. The first one will create a copy, so seond is the way to go.



You can create a simple exception class and try it so that the copy constructor / constructor is printed to the console when they run. So you should see the difference.

-2


a source







All Articles