0% found this document useful (0 votes)
41 views2 pages

Lecture 6

Uploaded by

ARVIND H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Lecture 6

Uploaded by

ARVIND H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Handling Errors and Exceptions: Errors, Exceptions

Error handling is an essential procedure in Visual Basic programming because it can help
make the program error-free. An error-free program can run smoothly and efficiently, and the
user does not have to face all sorts of problems such as program crash or system hang

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. VB.Net
exception handling is built upon four keywords - Try, Catch, Finally and Throw.
 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

Try
[ tryStatements ]
[ Exit Try ]
[ Catch [ exception [ As type ] ] [ When expression ]
[ catchStatements ]
[ Exit Try ] ]
[ Catch ... ]
[ Finally
[ finallyStatements ] ]
End Try

Example

Module exceptionProg
Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
Dim result As Integer
Try
result = num1 \ num2
Catch e As DivideByZeroException
Console.WriteLine("Exception caught: {0}", e)
Finally
Console.WriteLine("Result: {0}", result)
End Try
End Sub
Sub Main()
division(25, 0)
Console.ReadKey()
End Sub
End Module

You might also like