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.
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.
' 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
Because VB.NET supports multiple Catch keywords, we have the ability to check for both exceptions! Here is an example demonstrating this concept.
' 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!

Add Reply





MultiQuote

| 


