0% found this document useful (0 votes)
18 views

Chapter Four

The document discusses exception handling in VB.Net. It introduces exceptions, the keywords used for exception handling like Try, Catch, Finally and Throw, and some common exception classes. It also provides examples of implementing exception handling in code.

Uploaded by

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

Chapter Four

The document discusses exception handling in VB.Net. It introduces exceptions, the keywords used for exception handling like Try, Catch, Finally and Throw, and some common exception classes. It also provides examples of implementing exception handling in code.

Uploaded by

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

Chapter 4: Exception Handling

4.1 Introducing 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.

An Exception in VB.Net 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.

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.
4.2 Structured Exception Handling

VB.NET utilizes the .NET Framework’s standard mechanism for error reporting, called
Structured Exception Handling; it relies on exceptions to report errors that arise in applications.
Exceptions are classes that trap the error information. To utilize .NET’s Structured Exception
Handling mechanisms properly, developers need to write smart code that watches out for
exceptions and implement code to deal with these exceptions.

 The following table provides some of the predefined exception classes derived from the
Sytem.SystemException class −

Exception Class Description

System.IO.IOException Handles I/O errors.

System.IndexOutOfRangeException Handles errors generated when a method refers to an array


index out of range.

System.ArrayTypeMismatchException Handles errors generated when type is mismatched with the


array type.

System.NullReferenceException Handles errors generated from deferencing a null object.

System.DivideByZeroException Handles errors generated from dividing a dividend with zero.

System.InvalidCastException Handles errors generated during typecasting.

System.OutOfMemoryException Handles errors generated from insufficient free memory.

System.StackOverflowException Handles errors generated from stack overflow.

4.3 Implementing Exception Handling


Example1:
Module Module1
Sub Main()
Try
Throw New ApplicationException("Throwing a custom exception")
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
Console.WriteLine("The Finally Block")
End Try
Console.ReadKey()
End Sub

End Module

Example 2:
Module Module1
Sub division(ByVal n1 As Integer, ByVal n2 As Integer)
Dim answer As Integer
Try
answer = n1 \ n2
Catch ex As DivideByZeroException
Console.WriteLine("Exception: {0}", ex)
Finally
Console.WriteLine("Answer is: {0}", answer)
End Try
End Sub
Sub Main()
division(4, 0)
Console.ReadKey()
End Sub

End Module

Windows Form Based Example

You might also like