28

Is it proper to use throw new FoobarException(Baz argument); or throw FoobarException(Baz argument);?

When catching I always use catch(FoobarException& e) "just in case" but I never could find a solid answer whether I had to use new or not in C++ (Java definitely) or if it was just a preference of the programmer.

Casey
  • 10,297
  • 11
  • 59
  • 88

2 Answers2

35

Exceptions in C++ should be thrown by value, and caught by reference.

So this is the proper way:

try
{
    throw FoobarException(argument);
}
catch( const FoobarException &ex )
{
    cout << ex.what() << endl;
}

Don't throw an exception created with new, since who's responsible for deleting it is not well-defined. In addition, performing allocations during error handling can throw another exception, obscuring the original problem.

You don't have to catch by const reference (non-const will work fine), but I like doing it anyway. You should however always by reference (not by value) to catch the exception polymorphically. If you don't, the exception's type could be sliced.

Sven
  • 21,903
  • 4
  • 56
  • 63
  • Why exception should always be caught by reference not by value? Are you saying about object slicing problem because of passing derived object by value to a method expects argument of base class object? – Destructor May 02 '15 at 14:10
8

unless there is some special requirement not to, I always throw by value and catch by const reference. This is because the new itself could throw an exception as well, during error handling, it is best to avoid things which can cause exceptions.

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
  • That's right, throwing an exception while another exception is active is essentially forbidden (I think it's UB), so standard `new` is out. You could use some non-throwing allocation if you really had to, but that's still insane :-) – Kerrek SB Jul 23 '11 at 17:13
  • 1
    My 2c's: Just to add to @KerrekSB 's sage advice, it is actually defined behaviour (see http://en.cppreference.com/w/cpp/error/terminate). The aforementioned link also points to a number of other things that are best avoided (such as throwing exceptions from destructors, because then you may wind up raising exceptions while the stack is being unwound for a previously thrown exception). – Rob Apr 08 '14 at 14:58