Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Wrapping exceptions & Debugging ASP.NET

I am experimenting with the System.Diagnostics.Trace class.

I was hoping to do something like the following:
//IN THE CODEBEHIND
private void myMethod()
{
  //...
  try
  {
  //create some error
  }
  catch (System.Exception ex)
  {
  throw new System.Exception("Some dumb exception", ex);
  }
}

//In global.asax.cs
protected void Application_Error(Object sender, EventArgs e)
{
  System.Exception ex = System.Web.HttpContext.Current.Server.GetLastError().GetBaseException();
  System.Diagnostics.Trace.Write(ex.Message);
  System.Diagnostics.Trace.Write(ex.InnerException.ToString());

}

This doesn't do what I hoped (ie: write "Some dumb error" and then the actual error).

The closest I got was in the code behind to write:

private void myMethod()
{
  //...
  catch
  {
      throw System.Web.HttpExcepton.CreateFromLastError("Some dumb error");
  }
}

At least with that I get my error string...

Any thoughts on this are most welcome.

Thanks,

Charles

Charles Reich
Thursday, March 31, 2005

The GetBaseException() method is iterating through the exception stack, skipping your "Some dumb exception" error and going straight to your, well, base exception.

protected void Application_Error(Object sender, EventArgs e)
{
    System.Exception ex = System.Web.HttpContext.Current.Server.GetLastError().InnerException;
    System.Diagnostics.Trace.Write(ex.Message);
    System.Diagnostics.Trace.Write(ex.InnerException.ToString());
}

or better yet, iterate through the exception stack.

protected void Application_Error(Object sender, EventArgs e)
{
    System.Exception ex = System.Web.HttpContext.Current.Server.GetLastError();
    while (ex.InnerException != null)
    {
        System.Diagnostics.Trace.Write(ex.Message);
        ex = ex.InnerException;
    }
    System.Diagnostics.Trace.Write(ex.ToString());
}

moron
Thursday, March 31, 2005

Thanks for pointing me in the right direction.

Looping through the InnerException's was the way to go.

Charles Reich
Tuesday, April 05, 2005

*  Recent Topics

*  Fog Creek Home