Define Exception handling in ASP.NET.
- Exceptions or errors are unusual occurrences that happen within the logic of an application.
- The CLR provides structured way to deal with exceptions using Try/Catch block.
- ASP.NET also supports exception handling through server events such as Page_Error and Application_Error events.
Try:- It identifies a block of code for which particular exceptions is activated.
- It is followed by one or more catch blocks.
- It can be a compound statement.
Catch:- It catches an exception with an exception handler at the place in a program where you want to handle the problem.
- It indicates the catching of an exception.
- There are multiple Catch blocks permitted.
Finally:- It is used to execute a given set of statements, whether an exception is thrown or not thrown.
Syntax:try
{
//Statements
}
catch(ExceptionName e1)
{
//Error handling code
}
catch(ExceptionName e2)
{
//Error handling code
}
catch(ExceptionName eN)
{
//Error handling code
}
finally
{
//Statements to be executed
}
- Page and application error event handler is useful in cases where an exception is unexpected and does not occur within the bounds of a Try/Catch/Finally block.
- These exceptions are usually an indicator that something is substantially wrong with the application.
Example:public void Page_Error(Object sender, EventArgs e)
{
//Implementation here
}
- This event is implemented within the page where the error may occur.
public void Application_Error(Object sender, EventArgs e)
{
//Implementation here
}