stu nicholls dot com | menu - Professional dropdown #3

click here


Exception Handling


Structured and Unstructured Exception Handling Unstructured Exception Handling
Using Resume Next:
Module Module1
Sub Main()
Dim i=0, j=1, k As Integer
On Error GoTo Handler
k=j/i
System.Console.WriteLine(“Program Continued and completed”)
Exit Sub
Handler:
System.Console.WriteLine(“Divide By Zero”)
Resume Next
End Sub
End Module
Example
Module Module1
Sub Main()
Dim i=0, j=1, k As Integer
On Error GoTo Handler
k=j/i
Exit Sub
Handler:
System.Console.WriteLine(“Divide By Zero”)
End Sub
End Module







Structured Exception Handling
Example2:
Module Module1
Sub Main()
Dim i=0, j=1, k As Integer
try
k=j/i
System.Console.WriteLine(“The result is” & k)
Catch
System.Console.WriteLine(“Over flow”)
End Try
End Sub
End Module
Example:1
Module Module1
Sub Main()
Dim i=0, j=1, k As Integer
try
k=j/i
System.Console.WriteLine(“The result is ” & k)
Catch e As Exception
System.Console.WriteLine(e.Message)
End Try
End Sub
End Module



Using Finally

The code in the Finally block, if there is one, is always executed in a Try..Catch..Finally statement, even if there is no exception. This block is executed.

Example:
Module Module1
Sub Main()
Dim i=0, j=1, k As Integer
try
k=j/i
System.Console.WriteLine(“The result is ” & k)
Catch
System.Console.WriteLine(“Over flow”)
Finally
System.Console.WriteLine(“In finally”)
End Try
End Sub
End Module


Using multiple Catch statement:

This can be done when you filter exceptions

Example:
Module Module1
Sub Main()
Dim i=0, j=1, k As Integer
try
k=j/i
System.Console.WriteLine(“The result is” & k)
Catch e As System.OverflowException
System.Console.WriteLine(“Over flow”)
Catch e As System.ArgumentException
System.Console.Writelin(“Invalid Arguments”)
Finally
System.Console.WriteLine(“In finally”)
End Try
End Sub
End Module

<< Previous   Next>>


comments powered by Disqus
click here
Footer1