2. C
O
N
T
E
N
T
01
Text Title
Place your own text
here
02 Exception
Handling
03
Unstructured Exception
Handling
04
Types of Exception
Handling
2
01 What is
Exception?
05 Structured Exception
Handling
5. EXCEPTION
An exception refers to a problem that arises
during program execution. It is brought about by
an unexpected circumstance. A good example is
when you are performing a division operation, and
then you divide by zero (0). An exception will be
raised.
5
7. Exception Handling
7
Exception handling is the process of
responding to the occurrence,
during computation, of exceptions(
anomalous or exceptional conditions
requiring special processing ) often
disrupting the normal flow
of program execution.
12. On Error GoTo :
Public Class Form1
Dim num1 As Integer
Dim num2 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim result As Integer
On Error GoTo lbl
num1 = CInt(TextBox1.Text)
num2 = CInt(TextBox2.Text)
funA()
result = num1 num2
lbl:
MsgBox(result)
End Sub
13. If…else :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim array As Integer() = New Integer(3) {}
array(0) = 10
array(1) = 20
array(2) = 30
array(3) = 40
For i As Integer = 0 To array.Length - 1
MessageBox.Show(array(i))
Next
End Sub
End Sub
15. Structured Exception
Handling
15
With exception handling, you can
transfer the control of a program from
one part to another.
In VB.NET, exceptions are handled in
Structured way using 4 keywords:
17. Keyword Detail
Try
The work of the Try block is to identify the code
block for which a specific exception will be
activated. It should be followed by a catch
block(s).
Catch
Catching of the Exception is done in this block. It
is an exception handler in which the Exception
is handled.
Finally
Use the Finally block to run a set of statements
whether an exception has occurred or not.
Throw
An exception is thrown after the occurrence of a
problem. This is the work of the Throw keyword.
18. Syntax of Try/Catch
18
Try
[ try_Statement(s) ]
[ Exit Try ]
[ Catch [ exception_name [ As type ] ] [ When expression ]
[ catch_Statement(s) ]
[ Exit Try ] ]
[ Catch ... ]
[ Finally
[ finally_Statement(s) ] ]
End Try
19. Public Class Form1
Dim num1 As Integer
Dim num2 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim result As Integer
Try
num1 = CInt(TextBox1.Text)
num2 = CInt(TextBox2.Text)
funA()
result = num1 num2
Catch ex As DivideByZeroException
MsgBox(ex.Message)
Finally
MsgBox(result)
End Try
End Sub
Public Function funA() As Integer
Dim result As Integer
num1 = CInt(TextBox1.Text)
num2 = CInt(TextBox2.Text)
If num2 = 0 Then
Throw New DivideByZeroException("plz enter denominator greater then 0")
End If
result = num1 num2
End Function
End Class