R
Rick
I have created a custom exception class for my application. In it, I want to
be able to automatically output the ToString() from the object causing the
exception. (I use the ToString() to output the state of my objects to help
with debugging). However, the ToString() method, being often overriden, may
itself cause an exception, in which case a try-catch block does not work.
How can I handle exceptions in this situation... or is this even the right
question? Any other way to achieve this?
public class MyException : ApplicationException
{
public MyException(string message, object source, Exception
innerException)
: base(MakeMessage(message, source), innerException){}
private static string MakeMessage(string message, object source)
{
string Msg = message;
if(source != null)
{
try{Msg = message + " [ SOURCE: " + source.ToString() + " ]";}
catch{Msg = message + " [ Source: " + source.GetType().Name +
" ]";}
}
return Msg;
}
}
be able to automatically output the ToString() from the object causing the
exception. (I use the ToString() to output the state of my objects to help
with debugging). However, the ToString() method, being often overriden, may
itself cause an exception, in which case a try-catch block does not work.
How can I handle exceptions in this situation... or is this even the right
question? Any other way to achieve this?
public class MyException : ApplicationException
{
public MyException(string message, object source, Exception
innerException)
: base(MakeMessage(message, source), innerException){}
private static string MakeMessage(string message, object source)
{
string Msg = message;
if(source != null)
{
try{Msg = message + " [ SOURCE: " + source.ToString() + " ]";}
catch{Msg = message + " [ Source: " + source.GetType().Name +
" ]";}
}
return Msg;
}
}