-1

So I've created a custom exception class (let's call it CustomException) along with some custom properties not found in the Exception class. In the global.asax.cs file there is the Application_Error method that is called whenever an exception happens. I'm using Server.GetLastError() to grab the exception that triggered the Application_Error method. The problem is that Server.GetLastError() only grabs an Exception object and not the CustomException object that is being thrown along with its custom properties. Basically the CustomException is stripped down to an Exception object when retrieved by Server.GetLastError(), thus losing the custom properties associated with CustomException.

Is there a way for GetLastError() to actually retrieve the CustomException object and not the stripped down Exception version? This is needed in order to store the errors in a database table with more information than would normally be supplied by an Exception.

Application_Error:

protected void Application_Error(object sender, EventArgs e)
{
    // This var is Exception, would like it to be CustomException
    var ex = Server.GetLastError();           

    // Logging unhandled exceptions into the database
    SystemErrorController.Insert(ex);

    string message = ex.ToFormattedString(Request.Url.PathAndQuery);

    TraceUtil.WriteError(message);
}

CustomException:

public abstract class CustomException : System.Exception
{        
    #region Lifecycle

    public CustomException ()
        : base("This is a custom Exception.")
    {
    }

    public CustomException (string message)
        : base(message)
    {
    }

    public CustomException (string message, Exception ex)
        : base(message, ex)
    {
    }

    #endregion

    #region Properties

    // Would like to use these properties in the Insert method
    public string ExceptionCode { get; set; }
    public string SourceType { get; set; }
    public string SourceDetail { get; set; }
    public string SystemErrorId { get; set; }

    #endregion        
}
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Austin M
  • 11
  • 1
  • 1
    Have you tried verifying with the debugger what is the actual type of `ex` or perhaps its `InnerException` property? – BartoszKP Jul 31 '14 at 20:02
  • It depends on `SystemErrorController.Insert(ex);`'s implementation, having your specific type here does not help when you pass it in to `Insert`, Also your exception really should have [a protected constructor that calls the base protected constructor and be marked as `Serializeable`](http://msdn.microsoft.com/en-us/library/vstudio/ms229064(v=vs.100).aspx). – Scott Chamberlain Jul 31 '14 at 20:02

1 Answers1

0

Just cast the Server.GetLastError's result to CustomException:

var ex = Server.GetLastError() as CustomException;

Remember that in some situations your CustomException could not be the top exception in the StackTrace, in this case you'll need to navigate through the InnerExceptions to find the right one.

Please, check the @scott-chamberlain links about how to design custom exceptions.

giacomelli
  • 7,287
  • 2
  • 27
  • 31