I am far to be a specialist of C++ ( I used C++ since VC++ 4 up to 6 when I was tired of VB , but I have stopped since the release of VC# in 2002 ) but maybe this link could be useful.
A good rule for every code I am writing ( VC#/VB ) : don't forget the treatment of the exception.
I am using this code in VC# ( rarely modified since 2003 )
public static void TreatException(Exception p_ex)
{
Int32 _i = 0;
String _s = "";
Exception _ex = null;
ErrorsCount++;
ShortMessage = "Error " + ErrorsCount.ToString() + " A (generic) exception occured in " + ErrorPlace;
_s = Nl + NowToString() + Nl;
_s += "********** " + ShortMessage + " **********" + Nl;
_s += "Message : " + p_ex.Message + Nl;
_s += "GetBaseException : " + p_ex.GetBaseException().Message + Nl;
_s += "HelpLink : " + p_ex.HelpLink + Nl;
_s += "Source : " + p_ex.Source + Nl;
_s += "TargetType : " + p_ex.TargetSite.Name + Nl;
_s += "Data : ";
if ( p_ex.Data == null )
{
_s += "N/A" + Nl;
}
else
{
_s += Nl;
foreach ( DictionaryEntry _de in _ex.Data )
{
_s += " - Key : " + _de.Key.ToString() + Nl;
_s += " Value : " + _de.Value.ToString() + Nl;
}
}
_s += "InnerException : ";
if ( p_ex.InnerException == null )
{
_s += "N/A" + Nl;
}
else
{
_s += Nl;
_ex = p_ex.InnerException;
_i = 1;
while ( _ex != null )
{
_s += " Inner" + _i.ToString().PadRight(2) + ":" + Nl;
_s += " - Message : " + _ex.Message + Nl;
_s += " - Type : " + _ex.GetType().FullName + Nl;
_s += " - Data : ";
if ( _ex.Data == null )
{
_s += "N/A" + Nl;
}
else
{
_s += Nl;
foreach ( DictionaryEntry _de in _ex.Data )
{
_s += " - Key : " + _de.Key.ToString() + Nl;
_s += " Value : " + _de.Value.ToString() + Nl;
}
}
_ex = _ex.InnerException;
}
}
_s += "StackTrace : " + Nl + p_ex.StackTrace + Nl;
_s += "End of treatement of the error " + ErrorsCount.ToString() + Nl + Nl;
ErrorMessage = _s;
AppWriteLog(false , ErrorMessage);
}
AppWriteLog is a method writing the ErrorMessage value in an application logfile. Nl is ony the carriage linefeed of this logfile. And I am interested only on the most internal exception ( the last one ). Maybe complicated but I am avoiding to loose time. If a specific exception is often happening , don't hesitate to create a method which is treating it without forgetting the specific properties/methods of it. I have provided some code on the MSDN/Technet forums for Systm.Data.SqlClient.SqlException ( Line , LineNumber , Procedure , ... ) or Microsoft.SqlServer.Management.Smo.SmoException ( for the ProdVer and SmoExceptionType properties ) since 2006.