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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s