I have some custom error classes defined, each with their own functionality (not shown):
Exceptions.cs
public abstract class MyAppException : Exception {
//...
}
public class ValidationException : MyAppException {
//...
}
public class AccessDeniedException : MyAppException {
//...
}
Now in the codebehind of a blank page I have:
test.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
throw new AccessDeniedException();
}
My intention is to catch this at the application level:
Global.asax.cs
protected void Application_Error(object sender, EventArgs e) {
var exc = Server.GetLastError();
if (exc is MyAppException) ((MyAppException)exc).Log();
}
But by adding a breakpoint I find that exc is MyAppException evaluates as false.
Where am I going wrong?
Try the InnerException:
It’s the
System.Web.UI.Page.HandleErrormethod that wraps the actual exception into anHttpUnhandledException.