Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Exception Handling in .Net Framework

Author: Faraz

This article will describes you, about the Exception handling in .Net Framework. In this, I’ll discuss the basic syntax and properties that can be used. Read on to learn more!

Exceptions are caught using the Try, Catch, Finally constructs. In all .Net languages the construct of Exception and its work is same. The syntax is as follows:

 

 

    Try

    ' Code that may fail goes here

    Catch Exp1 As ExceptionType

    ' Handle ExceptionType

    Catch Exp2 As ExceptionType

    ' Handle ExceptionType

    Catch

    ' Handle Expetion that gets here

    Finally

    ' Code that always Execute

    End Try

 

Note that Try block is placed around code that may give rise to an exception. And one or more Catch clauses are declared to handle exceptions. You can’t have a Try without at least one Catch clause, and you can’t have Catch clauses outside a Try block.

 

Exceptions are tagged with an exception object, and Catch clauses can be constructed to catch a particular class of exception, as in:

 

    Catch Exp1 As ExceptionType

 

All exception objects must be from classes that inherit from System.Exception. Once you’ve caught an exception, you can use its members to find out about the exception. The StackTrace property returns the stack trace (which tells you where the exception occurred) as a String; the Message property retrieves the message associated with the exception; the Source property returns a string indicating the name of the application or object that generated this exception; and TragetSite is property that tells you which method threw the exception:

 

    Try

      obj.Method()

    Catch Exp As NullReferenceException

      Console.WriteLine("Stack Trace: ", Exp.StackTrace);

      Console.WriteLine("Message: ", Exp.Message);

      Console.WriteLine("Source: ", Exp.Source);

      Console.WriteLine("TargetSite: ", Exp.TargetSite);

    End Try

 

Because of the way that inheritance works, you can use Exception to catch any exception type, as it’s the base class for all exceptions. If you use multiple Catch clauses, you need to be careful that one Catch does not hide another:

 

    Try

    ' Code that may fail goes here

    Catch Exp1 As ExceptionType

    ' Handle ExceptionType

    Catch Exp2 As ExceptionType

    ' Handle ExceptionType

    End Try

 

Because all exceptions are Exception objects, all exception objects will be caught by the first clause and nothing will get through to the second. Note that the C# compiler will warn you if you try to do this, but VB won’t.

 

It is also possible that the Try/Catch blocks inside one another, provided that you nest a complete Try/End Try construct, although this isn’t done very often in practice because it can lead to code that is hard to read.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Using System.DirectoryServices to Access ASDI provider for IIS in VB.NET (Part I)

Disclaimer - Privacy
© 2002-2017 DevASP.net