Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a throw in catch(...) throw by value or by reference

Tags:

c++

My boss answered this question why ... (three points) in catch block is exist? quite elegantly.

But it's made me think of something (and hopefully makes up for my previous bad question), does

catch(...){
    throw;
}

rethrow the caught exception by value (i.e. a deep copy is taken), or by reference?

like image 250
P45 Imminent Avatar asked Apr 20 '16 08:04

P45 Imminent


People also ask

Why is it preferred to catch exceptions by reference instead of by value?

Catching by value makes a copy of the exception. But you don't rethrow the copy. You rethrow the original exception that was copied. As such, any modification to the exception caught by value will be lost, including the slicing.

Which of the following defines throwing and handling exceptions in C ++? Throw by a value and catch by a reference?

Explanation: Exception handler is used to handle the exceptions in c++.

Can we use catch without throw?

Hi Shola, yes you can use try / catch without a throw.

What does throw and catch do C++?

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.


1 Answers

The standard says:

A throw-expression with no operand rethrows the currently handled exception. The exception is reactivated with the existing temporary; no new temporary exception object is created.

-- ISO/IEC 14882:2011 Section 15.1 par. 8

In other words, it simply continues the exception propagation with the original exception object. I suppose this is analogous to what you mean by "by reference".

like image 92
Andrew Avatar answered Oct 01 '22 22:10

Andrew