What about implementing an own exception class MyBaseException deriving from
ApplicationException ? If you log the source of the exception and the
message within MyBaseException's constructor, all is automatically logged.
You could also enhance the logging by recursing through all inner exceptions
....
public class MyBaseException : ApplicationException
{
private string m_Source;
private System.Exception m_InnerException;
public MyBaseException(string source, string message) : base(message)
{
this.m_Source = source;
// use static log function ...
LogFile.Write(this.Source + "(): " + this.Message);
}
public MyBaseException(string source, string message, System.Exception
innerException) : base(message, innerException)
{
this.m_Source = source;
// use static log function ...
if (this.InnerException != null)
LogFile.Write(this.Source + "(): " + this.Message + "[" +
this.InnerException.Message + "]");
else
LogFile.Write(this.Source + "(): " + this.Message);
}
public string Source
{
get
{
return this.m_Source;
}
}
}
public class MathClass
{
public static double Mul(double x, double y)
{
try
{
return checked(x * y);
}
catch (System.Exception ex)
{
throw new MyBaseException("MathClass.Mul", "Unexpected
exception", ex);
}
}
public static double Div(double x, double y)
{
try
{
if (y == 0)
throw new MyBaseException("MathClass.Div", "Division by zero
forbidden");
return checked(x / y);
}
catch (MyBaseException)
{
throw;
}
catch (System.Exception ex)
{
throw new MyBaseException("MathClass.Mul", "Unexpected
exception", ex);
}
}
}
Greetings, Christian