520 likes | 1.07k Views
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.
E N D
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_Errorand 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.
Object Exception IndexOutOfRangeException NullReferenceException ArgumentException ExternalException SystemException ApplicationException Exception Hierarchy
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
Multiple Catch Blocks Dim x AsInteger = 0 Dim div AsInteger = 0 Try div = 100 / x Catch de As DivideByZeroException ‘ Statements for handling the exception. Catch ee As Exception ‘ Statements for handling the exception. Finally ‘ Any cleanup code End Try
Catching all Exceptions Try ‘Stmts Catch e As Exception ‘Stmts End Try Try ‘Stmts Catch ‘Stmts End Try
User-Filtered Clauses Dim t As Integer Try t = 10 ThrowNew Exception CatchWhen 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 EndTry
Throwing an Exception • Throw exception_obj • Example Try ThrowNew DivideByZeroException(“Invalid Division”) Catch e As DivideByZeroException ‘Stmts End Try
Re-throwing an Exception Public Class MyTestClass Public Sub Method() Try Dim x As Integer = 0 Dim sum As Integer = 100 / x Catch ex As Exception Throw EndTry EndSub End 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 Sub End Class
Standard Exceptions • System.OutOfMemoryException • System.NullReferenceException • System.InvalidCastException • System.ArrayTypeMismatchException • System.IndexOutOfRangeException • System.ArithmeticException • System.DevideByZeroException • System.OverFlowException
User-Defined Exception Public Class EmployeeNotFoundException Inherits ApplicationException Public SubNew() 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