Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,172 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,927 people online right now. Registration is fast and FREE... Join Now!




Exception Handling in VB.NET

 
Reply to this topicStart new topic

> Exception Handling in VB.NET, How to handle exceptions in VB.NET

RodgerB
Group Icon



post 25 Dec, 2007 - 02:37 AM
Post #1


Exception Handling in VB.NET

In this tutorial, we will be learning all about exception handling in VB.NET. The topics we will be covering include:
  • Try, Catch and Finally keywords.
  • Giving the user information about an exception.
  • Catching only certain exceptions.

1) Try, Catch and Finally keywords.

The Try, Catch and Finally keywords are usually refered to as Error Handling. Handling exceptions with these methods will stop your application from recieving runtime errors (errors encountered during running the application) caused by exceptions.

When you handle these errors, VB.NET gives you the ability to get a wealth of information about the exception and ways to let your application correspond according to them. Before we get into getting exception information, lets talk the complete basics.
  • Try - The Try keyword will tell the compiler that we want to handle an area for errors. The Try keyword must have an Catch method, and must be encapsulated in an End Try statement.
  • Catch - The Catch keyword tells the application what to do if an error occurs. It will stop the application from crashing, and let the application do the rest.
  • Finally - The Finally keyword will occur regardless if the Try method worked successfully or a Catch method was used. The Finally keyword is optional.
Here is an example of a basic Try..Catch method:

CODE

Try
    ' Create a new integer variable.
    Dim anInteger As Integer = 0
    ' Divide zero by zero to produce a DivideByZeroException exception.
    anInteger \= 0
Catch ex As Exception
    ' Show an error telling the user an error occured.
    MessageBox.Show("An error occured")
Finally
    ' Tell the user that it has got to the Finally statement.
    MessageBox.Show("This has finally happened. :)")
End Try


Now, if you edit the above code where anInteger equals 1, and where anInteger \= 0 gets changed into anInteger \= 1, you will not get an error, and will skip to the Finally method.

2) Giving the user information about an exception.

Giving the user information about an exception is relatively easy. We will now invoke an exception by trying to use a malformed path on an IO function. This will be the circumstance an ArgumentException.

CODE

' Give user information about an exception.
Try
    ' We will purposely create a File Exception for the
    ' purpose of this example. We will be invoking the exception
    ' by providing a path of an illegal form.
    Dim fileContents As String = System.IO.File.ReadAllText("iShouldn'tWork :)")
Catch ex As Exception
    ' ex.ToString will give the user a large technical dump.
    MessageBox.Show("ToString: " & ex.ToString)
    ' ex.Message will give the user a brief, more user friendly error message.
    MessageBox.Show("Message: " & ex.Message)
End Try


You will notice we have changed Catch to Catch ex As Exception. By adding this, it will create a new 'ex' variable and get information about the exception. ex.ToString() will give the user a large technical dump of the error that occured. ex.Message will give a more to the point error message that doesn't look fugly.

3) Catching only certain exceptions.

Sometimes its good to be precise about what kind of exception you are trying to check for. Say we want to check if there is a DivideByZeroException exception, but there could be a risk of an ArgumentException with a malformed path tongue.gif. What do we do?

Because VB.NET supports multiple Catch keywords, we have the ability to check for both exceptions! Here is an example demonstrating this concept.

CODE

' Give user information about an exception.
Try
    ' Create a new integer variable.
    Dim anInteger As Integer = 0
    ' Divide zero by zero to produce a DivideByZeroException exception.
    anInteger \= 0

    ' Purposely provide an invalid argument to produce a ArgumentException.
    Dim fileContents As String = System.IO.File.ReadAllText("iShouldn'tWork :)")

    Catch exDivide As DivideByZeroException
        ' Show an error telling the user a DivideByZeroException occured.
        MessageBox.Show("The application tried to divide by zero!")
    Catch exArgs As ArgumentException
        ' Show an error telling the user an ArgumentException occured.
        MessageBox.Show("The application tried to provide an invalid argument!")
    Finally
        ' Tell the user that it has got to the Finally statement.
        MessageBox.Show("That was intense! :D")
End Try


If a DivideByZeroException is thrown, it will be caught and will skip to the Finally method (same goes for the ArgumentException). Modify the above code to alternate between exceptions etc.

Thus concludes this tutorial on Exception Handling in VB.NET. If you require any further assistance, make sure to post your question in the VB.NET board. Have Fun! smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Locke37
Group Icon



post 2 Jun, 2008 - 09:06 AM
Post #2
Heh...this was EXACTLY what I was looking for. I know about try{}...catch{} in java, but didn't know how to do it in VB.NET. And I was doing a project, and it kept halting execution when it hit an error...well, now it doesn't, thanks to you!

Nice work! Keep it up!
icon_up.gif biggrin.gif
Go to the top of the page
+Quote Post

gever
*



post 15 Oct, 2008 - 03:18 AM
Post #3
try more on exceptions

http://vb.net-informations.com/language/vb..._exceptions.htm

bolt.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/2/08 12:41AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month