0% found this document useful (0 votes)
13 views6 pages

Chapter 4 Exception Handling

Here is exception handling material use it.

Uploaded by

jamsibro140
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)
13 views6 pages

Chapter 4 Exception Handling

Here is exception handling material use it.

Uploaded by

jamsibro140
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/ 6

Chapter Four: Exception Handling

4.1. Introduction
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.
1) 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.
2) 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.
3) 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.
4) Throw: A program throws an exception when a problem shows up. This is done using a
Throw keyword.

4.2. Exception Classes in .Net Framework


In the .Net Framework exceptions are represented by classes. The exception classes in .Net
Framework are mainly directly or indirectly derived from the System.Exception class. Some
of the exception classes derived from the System.Exception class are
 System.ApplicationException and
 System.SystemException classes.
The System.ApplicationException class supports exceptions generated by application
programs. So the exceptions defined by the programmers should derive from this class.
The System.SystemException class is the base class for all predefined system exception.

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

Compiled By: Tizazu B(MSc in Software Engineering) Page 1 of 6


Exception Class Description

System.IO.IOException Handles I/O errors.


Handles errors generated when a method refers to
System.IndexOutOfRangeException
an array index out of range.

Handles errors generated when type is


System.ArrayTypeMismatchException
mismatched with the array type.

Handles errors generated from deferencing a null


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

System.InvalidCastException Handles errors generated during typecasting.

Handles errors generated from insufficient free


System.OutOfMemoryException
memory.

System.StackOverflowException Handles errors generated from stack overflow.

4.3. Handling Exceptions


VB.Net provides a structured solution to the exception handling problems in the form of try
and catch blocks. Using these blocks the core program statements are separated from the error-
handling statements.
These error handling blocks are implemented using the Try, Catch and Finally keywords.
Following is an example of throwing an exception when dividing by zero condition occurs:

4.3.1. Creating User-Defined Exceptions


You can also define your own exception. User defined exception classes are derived from the
ApplicationException class. The following example demonstrates this:

Compiled By: Tizazu B(MSc in Software Engineering) Page 2 of 6


4.3.2. Throwing Objects
You can throw an object if it is either directly or indirectly derived from the System.Exception
class.
Module exceptionProg
Sub Main()
Try
Throw New ApplicationException("A custom exception _
is being thrown here...")
Catch e As Exception
Console.WriteLine(e.Message)
Finally
Console.WriteLine("Now inside the Finally Block")
End Try
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces following result:
A custom exception is being thrown here...
Now inside the Finally Block

Compiled By: Tizazu B(MSc in Software Engineering) Page 3 of 6


4.4. VB.NET - File Handling
A file is a collection of data stored in a disk with a specific name and a directory path. When a
file is opened for reading or writing, it becomes a stream.
The stream is basically the sequence of bytes passing through the communication path. There
are two main streams: the input stream and the output stream. The input stream is used for
reading data from file (read operation) and the output stream is used for writing into the file
(write operation).

4.5. VB.Net I/O Classes


The System.IO namespace has various class that are used for performing various operation
with files, like creating and deleting files, reading from or writing to a file, closing a file etc.
The following table shows some commonly used non-abstract classes in the System.IO
namespace:

Compiled By: Tizazu B(MSc in Software Engineering) Page 4 of 6


4.6. The FileStream Class
The FileStream class in the System.IO namespace helps in reading from, writing to and closing
files. This class derives from the abstract class Stream.

Compiled By: Tizazu B(MSc in Software Engineering) Page 5 of 6


Example:
The following program demonstrates use of the FileStream class:

Compiled By: Tizazu B(MSc in Software Engineering) Page 6 of 6

You might also like