Try … Catch … Finally Statement in VB.Net

Try … Catch … Finally Statement in VB.Net

Try … Catch … Finally statement enables you to handle the probable errors in your code while your code is still executing fine.

While some people think that programming is the art of writing code, others believe that it actually is the art of detecting errors in the code. Errors are the undeniable part of writing codes. In other words; how great programmer you be, errors would occur in your code. Hence; you need to be familiar with the methods to detect errors and to solve them.

Many errors (mostly syntax errors) can be solved manually. But what to do when your program detects a runtime error? Suppose you develop a software that has to access an external file from the user’s computer, but that file does not exist. What will happen??? Obviously your software will crash and stop execution.

So, what to do!???

Programming languages such as VB.Net support the use of statements like (Try … Catch … Finally) that are used to detect probable errors and help the developer to handle these probable errors while the code is still running perfectly fine. Further you will get to know about this statement in detail. But before that you have to have a basic knowledge about exceptions.

What are exceptions in .NET?

Basically errors will occur, no matter how great an application is developed. For example; an application has to access a database but the database is not accessible. Or arithmetic overflow such as dividing a number by zero, occurs in an application. When such errors are detected, the .NET framework gathers information about that errors and stores it in an object called Exception.

There are many types of exceptions and some of them are SqlException, OverFlowException, FileNotFoundException and etc. All the exceptions in .NET framework are classified under the class name exception which itself inherits from the system class.

Try … Catch Statement

To brief it, Try … Catch statement follows the below syntax;

Basically in Try block you type in the piece of code that may cause an error and in Catch block you type in possible solutions to that error. It is important to know that Try … Catch statement does not fix an error. It actually handles an error for the developer and prevents the application from crashing down.

Related: Errors in VB.NET

Here is a very basic example of Try … Catch statement:

Output:

1 Try ... Catch 2 Try ... Catch

Throwing specific type of Exception

In the example code above, the Catch block has thrown a general Exception object. When you define your exception object as such, it will handle any type of error that the .NET framework knows. However; sometimes you know that a specific block of code in try block can only result in a specific error. In such situations you can throw specific types of exceptions. Some Exceptions known to .NET framework are:

NullReferenceException (handles errors when the application is trying to refer to a null object)

InvalidCastException (handles errors generated during typecasting)

IndexOutOfRangeException (handles errors when a method tries to access an array index out of range)

IO.IOException (handles errors related to Input and Output)

OverFlowException (handles errors generated from stack overflow)

Check Also: Working with Multiple forms in VB.NET

Example code:

Output:

Finally Block & When keyword

Finally block is the third block in a Try … Catch … Finally statement. It is a very useful block but it is not compulsory to use. Just like the previous examples that we did not use. Finally block is used to execute a piece of code whether a proper solution is found in the Catch block for the error detected in the Try block or not.

When keyword is used with the Catch block to specify when you want the exception to be thrown.

Output:

You can use Try … Catch … Finally statement to prevent your application from crashing down. Although using it will eventually result in more productive and useful applications but overusing it will make your application slower and the execution time of your application will increase.

Previous MySQL Database Storage Engines
Next How to build a responsive site structure with bootstrap

About author

Bahonar Javid
Bahonar Javid 43 posts

<p>* Computer Application Student<br /> * Co-Founder of www.codejow.com<br /> * Love to code and Learn new languages<br /> * Enjoy playing football</p>

View all posts by this author →

You might also like