Exception Handling

Structured and Unstructured Exception Handling
  • Unstructured exception handling revolves around the On Error GoTo statement and structured exception handling uses Try…Catch…Finally statement
  • Without an On Error GoTo or Try.. Catch..Finally statement an exception that occurs is fatal and your program will stop
Unstructured Exception Handling
  • Code is difficult to read, debug and maintain
  • Errors may be overlooked
  • Syntax:

    On Error GoTo [line|0|-1]| Resume Next

    • GoTo Line- Enables the exception handling code that starts at the line specified in the required line argument. The line argument is any line number or label. The specified line must be in the same procedure
    • GoTo 0-Disables enabled exception handler in the current procedure and resets to nothing
    • GoTo -1 same above
    • Resume Next- Specifies that when an exception occurs, execution skips over the statement that caused the problem and goes to the statement immediately following. Execution continues from that point
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
  • Code is easy to read, debug and maintain
  • Allows you to create protected blocks of code
  • Allows nested Handling
  • Allows filtering of exceptions similar to select case statement
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


Procedures and Function << Previous

Next >> Adding new Forms to the project

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus






Footer1