9 messages in com.mysql.lists.plusplusRe: Catching Exceptions
FromSent OnAttachments
Adam Clauss26 May 2003 16:31 
Ben Clewett27 May 2003 00:42 
Markus Gerwinski27 May 2003 00:59 
Murr...@Comneon.com27 May 2003 01:16 
Markus Gerwinski27 May 2003 01:38 
Murr...@Comneon.com27 May 2003 01:46 
Adam Clauss27 May 2003 11:17 
Sinisa Milivojevic28 May 2003 03:52 
Warren Young28 May 2003 09:05 
Subject:Re: Catching Exceptions
From:Warren Young (war@etr-usa.com)
Date:05/28/2003 09:05:13 AM
List:com.mysql.lists.plusplus

Adam Clauss wrote:

I noticed that the exceptions were being caught by pointer. I recommend that you catch them by const reference, as you always should. It might make a difference with the MSVC++ compiler.

Exactly right. Any compiler that lets you catch an exception through a pointer like this is confusing pointer semantics with reference semantics. Sure, they may be implemented the same way under the hood, but at the C++ level, they're distinct.

Although... Why const reference? I've typically just done it by value.

Because if you catch an exception by value, a copy has to be made of the exception object. That's inefficient with most exception objects, and also usually unnecessary. Sometimes, a copy might even be impossible, such as in the case of a bad_alloc exception.

If you just need to call a why() method on the exception object or something simple like that, a const reference avoids the copy, and ensures that your code doesn't modify the original exception object.