Page 1 of 1

Exception Handling in VB.NET How to handle exceptions in VB.NET Rate Topic: -----

#1 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Posted 25 December 2007 - 03:37 AM

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:

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 :P. 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.

' 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! :)

Is This A Good Question/Topic? 1
  • +

Replies To: Exception Handling in VB.NET

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 02 June 2008 - 10:06 AM

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!
:^: :D
Was This Post Helpful? 0
  • +
  • -

#3 gever  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 29
  • Joined: 19-June 08

Posted 15 October 2008 - 04:18 AM

try more on exceptions

http://vb.net-inform..._exceptions.htm

bolt.
Was This Post Helpful? 0
  • +
  • -

#4 Umaid123  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 15-December 08

Posted 30 August 2009 - 10:44 AM

Good tutorial. I appreciate it. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1