VB.Net - SPLessons

VB.Net Exception Handling

Home > Lesson > Chapter 19
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

VB.Net Exception Handling

VB.Net Exception Handling

shape Description

An Exception is used to avoid the errors during the execution of the program. Exception Handling is a mechanism in the VB.Net to handle the problems occurs by the wrong code or by some other reasons. Because of the Exception Handling,  the execution may not stop.Following are the types of an errors.

shape Syntax

[vbnet] Try [ Statements ] [ Catch [ exception [ As type ] ] [ When expression ] [ Statements ] [ Finally [ Statements ] ] End Try [/vbnet] The following is the Description about the main parts in the above syntax. Try: Having the code where there is a chance to occur error. Catch: Having the related exception to activate if the error occurred. Finally: Having the code that must be executed does not matter whether the exception raised or not.

shape Classes

The following table shows you the different exceptions classes which are inherited from the System.Exception Namespace.
Exceptions Classes Description
System.OutOfMemoryExecption Used to Handle Errors Produce from insufficient memory
System.DivideByZeroExecption Used to Handle Errors Produce from dividend with zero
System.IndexOutOfRangeExecption Used to Handle Errors produces from array index out of range
System.NullReferenceExecption Used to Handle Errors produces from null object
System.StackOverflowExecption Used to Handle Errors Producer from Stack overflow
System.InvalidCastExecption Used to Handle Errors Producer from during typecasting
System.ArrayTypeMisMatchExecption Used to Handle Errors Producer from mismatched with the  array type
System.IO.IOExecption Used to Handle I/O Errors

shape Example

See the simple example given below to know "How to handle Exception". [vbnet] Module ExceptionHandlingExample Sub division(ByVal a As Integer, ByVal b As Integer) Dim Result As Integer Try result = a \ b Catch e As DivideByZeroException Console.WriteLine("Exception catched: {0}", e) Finally Console.WriteLine("Result: {0}", Result) End Try End Sub Sub Main() division(5, 0) 'Here, we are trying to divide with the zero Console.ReadKey() End Sub End Module [/vbnet] Output: Following is a result where exception occurred.

shape Example

Now, write the following code and try to execute the code. [vbnet] 'Code without Exception Handling Module Module1 Sub division(ByVal a As Integer, ByVal b As Integer) Dim Result As Integer Result = a \ b Console.WriteLine("Result: {0}", Result) End Sub Sub Main() division(5, 0) Console.ReadKey() End Sub End Module [/vbnet] Output: See the above figure and observe the difference between the with and without Exception Handling. If we don't use Exception Handling, then we will get interruption by the above error.

Summary

shape Points

  • Exception occurred data will be placed in try block.
  • Run time error means logical error.
  • Developer can use throw statement in catch block to through an object.