C# – Understanding Global Exception Handling

Unhandled exception bubble up until they are exposed to the end user (aka “yellow screen of death”)

Best place to handle exception is the nearest locale to the exception itself. However, You CAN handlig globally.

In global.asax


void Application_Error(object sender, EventArgs e){

//What just happened?

Exception ex = Server.GetLastError();

//ex will always be of type HttpUnhandledException.

//To get to the exception that CAUSED that to happen

//you will need to look at the ex.InnerException

var innerException = ex.InnerException;

//Handle a specific type of Error differently.

if(innerException.GetType() == typeof(ArgumentOutOfRangeException){

Server.Transfer("Error.aspx");

}

//You must do this if you want to hide the yellow page of death.... any existing exceptions after this point will send the end user to exception page.

//Server.ClearError();

}

 

Understanding Custom Exceptions

Inherit from Exception like so:

public class myCustomException : Exception{

..............................

}

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.