EXCEPTION HANDLING IN .NET
description
Transcript of EXCEPTION HANDLING IN .NET
EXCEPTION HANDLING IN .NET
The exceptions are anomalies that occur during the execution of a program.
They can be because of user, logic or system errors.
If a user do no provide a mechanism to handle these anomalies, the .NET runtime environment provide a default mechanism, which terminates the program execution.
Exception Handling is an in build mechanism in .NET framework to detect and handle run time errors.
Unhandled Exceptions- Redirecting the user to an error page.
Page level Application level
Page Level
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication.WebForm1“ errorPage=“/pageError/PageError.html” %>
In the web.config file<customErrors mode=“On" />
Application Level
In the web.config page <customErrors mode=“On|Off|RemoteOnly”
defaultRedirect=“url”> <error statusCode=“statuscode” redirect=“url” /> </customErrors >
Example: <customErrors mode=“On” defaultRedirect=“myerror.htm”> <error statusCode=“403” redirect=“authfailed.aspx” /> <error statusCode=“404” redirect=“filenotfound.aspx” /> </customErrors >
Page level would override Application level
Error handlers Application_Error and Page_Error, will get called before custom Errors is utilized.
Handling Exceptions
HTTP Module level by handling the HttpApplication.Error event.
Application level by handling the Application.Error event.
Page level by handling the Page.Error event.
Locally (method level), where exceptions could be thrown.
HTTP Module Level
Attaching an HTTP Module which would have a handler attached to the Application.Error event.
Application Level
Event : Application.Error Application_Error Event handler in
Global.asax.cs Inside error handler
Server.GetLastError Server.ClearError
Page Level
Event : Page.Error Private Sub Page_Error(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Error
End Sub
We can do … Exception ex = Server.GetLastError(); this.ErrorPage =
“/ErrorPages/BaseError.html”;
Local error handling
try-catch-finally
Local Exception Handling in VB.NET
In VB.NET, exceptions are nothing but objects of the type Exception.
The Exception is the ultimate base class for any exceptions in VB.NET.
The VB.NET itself provides couple of standard exceptions.
Or even the user can create their own exception classes.
Exception Hierarchy
Object
Exception
ApplicationException
SystemException
IndexOutOfRangeException
NullReferenceException
ArgumentException
ExternalException
Exception Class - Properties
StackTrace InnerException Message HelpLink
General form
Try‘ statement which can cause an exception.
Catch x As Type When filter-condition‘ Statements for handling the exception.
Finally‘ Any cleanup code
End Try
Uncaught Exceptions
Multiple Catch Blocks
Dim x As Integer = 0Dim div As Integer = 0Try
div = 100 / xCatch de As DivideByZeroException
‘ Statements for handling the exception.Catch ee As Exception
‘ Statements for handling the exception.Finally
‘ Any cleanup codeEnd Try
Catching all Exceptions
Try‘Stmts
Catch‘Stmts
End Try
Try‘Stmts
Catch e As Exception‘Stmts
End Try
User-Filtered Clauses
Dim t As Integer Try t = 10 Throw New Exception Catch When t = 10 t = t + 1 Catch ed As Exception t = t + 1 End Try
Combining Specific Exception & the User-Filtered Clauses
Dim t As Integer Try t = 10 Throw New Exception Catch ed As Exception When t =
10 t = t + 1 End Try
Throwing an Exception
Throw exception_obj Example
Try
Throw New DivideByZeroException(“Invalid Division”)Catch e As DivideByZeroException
‘StmtsEnd Try
Re-throwing an ExceptionPublic Class MyTestClass Public Sub Method() Try Dim x As Integer = 0 Dim sum As Integer = 100 / x Catch ex As Exception Throw End Try End SubEnd Class
Public Class MyTestClient Public Shared Sub Main() Dim mc As New MyTestClass Try mc.Method() Catch ex As Exception Console.WriteLine("Exception caught here") End Try End SubEnd Class
Standard Exceptions
System.OutOfMemoryException System.NullReferenceException System.InvalidCastException System.ArrayTypeMismatchException System.IndexOutOfRangeException System.ArithmeticException System.DevideByZeroException System.OverFlowException
User-Defined ExceptionPublic Class EmployeeNotFoundException
Inherits ApplicationException
Public Sub New()End Sub
Public Sub New(message As String)MyBase.New(message)
End Sub
Public Sub New(message As String, inner As Exception)MyBase.New(message,inner)
End Sub
End Class