Revision Question(testing use of Functions,textbox,lable)
Write a function procedure that computes the
area of a rectangle.
Write a procedure that makes use of the function
above to calculate the area of a rectangle. Let
the user input length and width of the rectangle
through textboxes and output the result (Area)
through a label.
Revision Question
Write code to implement the following array
Declare an array of 3 elements of type string
Assign each element of the array to one of the
students (name) in your class
Make use a For … next loop to print the
elements of the array on a listbox.
Exception Handling
An exception is a problem that arises
during the execution of a program.
An exception is a response to an
exceptional circumstance that arises while
a program is running, such as an attempt
to divide by zero.
Exceptions provide a way to transfer
control from one part of a program to
another.
The coding structure VB.NET used to deal with
such Exceptions is called the Try … Catch
structure
VB. Net exception handling is built upon
four keywords: Try, Catch, Finally and
Throw.
Uncaught exception
Program mostly terminates when there is an
uncaught exception (illustrate)
► If debugging in Visual Studio, application pauses
and Exception Assistant appears indicating where
the exception occurred
Dim n1, n2, r As Integer Take and assign
n1 = num1.Text inputs from user
n2 = num2.Text
r = n1 / n2
MessageBox.Show(r)
► Error Handling is an essential part of any good
code.
► Exception
An indication of a problem that occurs during a
program’s execution
System.Exception is the base class for all
exceptions
The coding structure VB.NET uses to deal with
such Exceptions is called the Try … Catch
structure.
Syntax Try …. Catch
Try
[ try statements ]
Catch ExceptionVariable As ExceptionType
[ catch statements ]
‘Additional Catch block
Finally
[ finally statements ]
End Try
Try Block
► Means “ Try to execute this code “
► Encloses code that might throw an exception and
the code that should not execute if an exception
occurs
► Corresponding End Try
► There must be at least one Catch block and/or
Finally block immediately after the Try block
Catch Block
► Means "Catch any errors here“
► Catches and handles an exception
Begins with keyword Catch
Specifies an identifier and exception type
► Example: Catch e As Exception
Executes when exception of proper type
matches
Finally Block
► Programs that obtain certain resources must
return them explicitly to avoid resource leaks
► Finally block
Optional in a Try statement
Placed after the last Catch block (if there is
one)
Executes whether or not an exception is
thrown in the corresponding Try block or any of
its corresponding Catch blocks
Generally we use it to close an opened file or
connection
Try
n1 = num1.Text
n2 = num2.Text
r = n1 / n2
MessageBox.Show(r)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
DivideByZeroTest
Try: A Try block identifies a block of code for which
particular exceptions will be activated. It's followed by
one or more Catch blocks.
Catch: A program catches an exception with an
exception handler at the place in a program where
you want to handle the problem. The Catch keyword
indicates the catching of an exception.
Finally: The Finally block is used to execute a given
set of statements, whether an exception is thrown or
not thrown. For example, if you open a file, it must be
closed whether an exception is raised or not.
Throw: A program throws an exception when a
problem shows up. This is done using a Throw
keyword.
Syntax
Assuming a block will raise an exception, a
method catches an exception using a
combination of the Try and Catch keywords.
A Try/Catch block is placed around the code that
might generate an exception.
Code within a Try/Catch block is referred to as
protected code, and the syntax for using
Class illustration
Public Class Form1
Private Sub bntAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handle
bntAdd.Click
Try
Dim Months() = {"Jan", "feb", "march"}
For i = 0 To 3
If i > Months.Length - 1 Then
Throw New IndexOutOfRangeException(" Your are accessing an index that don't
exist")
End If
lstAddArray.Items.Add(Months(i))
Next
Catch ex As Exception
MsgBox(ex.Message)
'Catch ex As Exception
' MsgBox("an error has occurred" & Environment.NewLine & ex.ToString())
Finally
MsgBox("am the finally")
End Try
Revision Questions
Outline the main function for each of the following
key words as used in exception handling.
i. Try
ii. Catch
iii. Finally
iv. Throw.