KEMBAR78
Exception Handling in VB.Net
EXCEPTION
HANDLING
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
What is
Exception?
1
3
ERROR
Compile
Time Error
Run Time
Error
Syntactical
Error
Logical
Error
Exception
Logical
Error
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
What is
Exception
Handling?
2
6
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.
Types of
Exception
Handling
3
8
Types
9
There are two types of Exception
Handling:
▪ Unstructured Exception Handling
▪ Structured Exception Handling
Unstructured
Exception
Handling
4
10
Unstructured Exception
Handling
11
There are two ways to handle
excceptions in Unstructured way :
 if …else
 On Error GoTo Statement
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
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
Structured
Exception
Handling
5
14
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:
Exception
ApplicationException SystemException
IndexOutOfRan
geException
Argument
Exception
Arithmetic
Exception
Exception Class
Hierarchy
ArgumentOutOfRangeExce
ption
DivideByZeroException
User-defined
Exception Class
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.
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
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
THANK YOU
20

Exception Handling in VB.Net