When catching an exception by reference is the only advantage that i get is avoiding a copy of the exception object being made? Basically the difference between
try
{
CString a_csSQL = _T("SELECT * FROM Library");
CDatabase aDB;
aDB.OpenEx(g_csConnectionStringWdDSN,CDatabase::noOdbcDialog));
aDB.ExecuteSQL(a_csSQL);
}
catch(CDBException *& ex)
{
ex->Delete();
}
AND
try
{
CString a_csSQL = _T("SELECT * FROM Library");
CDatabase aDB;
aDB.OpenEx(g_csConnectionStringWdDSN,CDatabase::noOdbcDialog))
aDB.ExecuteSQL(a_csSQL);
}
catch(CDBException * ex)
{
ex->Delete();
}
The difference between the two codes you posted is that the first one catches a pointer to an exception by reference, and the second one catches a pointer to an exception by value. In neither case is an exception copied, since you’re dealing with pointers.
In general, exceptions should be thrown by value, and caught by reference. The C++ standard library is designed with this expectation in mind. However, older libraries, (MFC for instance) throw exceptions by pointer as you do here, and are expected to be caught by pointer.
There’s no effective difference between catching a pointer by value and by reference, except that if you catch by reference that gives you the (completely useless) option of deleting the exception, allocating a new exception with that same pointer, and rethrowing the same exception-pointer.