Exceptions
  Catch an exception
  Throw an exception

Get URL for this page

How Do I...Catch an Exception?

This sample illustrates how to catch an exception. A try...catch...finally block is used in this sample. Any code that might throw an exception is placed inside of the try block. If an exception is thrown, the catch block is entered and the program can perform the appropriate operation to recover or alert the user. The code in the finally block is always executed and can perform cleanup after an exception has occurred.

 
Catch.aspx

[Run Sample] | [View Source]

Catching and dealing with exceptions is a standard part of any object-oriented program. A try...catch block consists of a statement that you are going to try to do something that you know might cause an exception to occur. If an exception occurs, control is immediately passed to your catch block, which can deal with the exception. If an exception does not occur, then your code will execute normally. As an option, you can add a finally block after your catch block, which contain code segments which will execute, whether any exceptions are raised or not.

The following code fragment demonstrates a try...catch...finally statement. The key points to note are the structure and the flow of control, depending on when an error may occur.


' This code shows how to catch an exception
Try
	Console.WriteLine("We're going to divide 10 by 0 and see what happens...")
	Console.WriteLine()

	Dim i as Integer = 10
	Dim j as Integer = 0
	Dim k As Integer k = i/j ' error on this line. Control will jump to the catch block...

Catch e As Exception  ' perform code which deals with the exception, or informs the user what occurred...
	Console.WriteLine("The following error occurred:")
	Console.WriteLine(e.ToString())  ' print the error message to the user...

Finally   ' this section will be performed, regardless of the above processing
	Console.WriteLine()
	Console.WriteLine("This statement is always printed")
End Try
VB

You can use the exception object in a number of ways. An exception has a number of properties that can help you identify help, source, or even stack information about an exception. This can be very useful for tracking down the original cause of the exception, or providing a better explanation of its source. If you do not think you are going to actually use the exception for any information, do not actually catch an object, even though you may still have a catch block. The following sample demonstrates the properties of an exception object and declares a catch block without an exception object.


' using properties of an exception object
Try
	'  ... Actions which may cause an exception go here

Catch e As Exception
	Console.WriteLine( e.Source )      ' the name of the application or object that caused the error
	Console.WriteLine( e.Message )     ' an alternate to using the ToString method, as above
	Console.WriteLine( e.StackTrace )  ' a string containing the stack trace for this exception
	Console.WriteLine( e.HelpLink )    ' a reference to a file which contains help information
End Try

' the following segment defines a try...catch block. Note that
' we do not actually define a usable Exception object

Try
    ' ... Actions

Catch                 ' no 'e As Exception' required, if you're not going to use the exception object
    ' ... Actions
End Try
VB

Why are these blocks important? Try making the above application without the try...catch statements. Clearly, this demonstration will always raise an exception. However, imagine you have a situation where an exception might occur. If you forget to include a try...catch block, then your application can crash on your users.

You can take the above examples one step further. We have dealt with the particular case of a non-specific exception. However, you might know in advance what kind of exception is going to occur. When you do, you can catch the anticipated exception, and do process it accordingly. You can then catch all other exceptions, and deal with them as well. The next sample demonstrates this.


' we are going to attempt to do some file processing. This can cause a number of potential exceptions
' Note that you can use the specific exceptions to get extra information about that exception,
' but this example is focused on showing control execution for those exceptions
Try
	File.Create("c:/temp/test.txt")  ' can fail for a number of reasons...

Catch ioe As IOException ' this error may occur if the temp directory does not exist...
	Console.WriteLine("An IO error occurred. It may be that the c:\temp folder does not exist")

Catch se As SecurityException ' you don't have the rights to make this action...
	Console.WriteLine("You don't have the security permissions to take this action!")

Catch e As Exception    ' catch all other exceptions...
	Console.WriteLine( e.ToString() )  ' simply print the standard exception information
End Try
VB

Summary

Catching exceptions is an essential part of object-oriented programming. Using try...catch statements is a simple way to make sure that your users are not confronted with unexpected crashes. You can get helpful information from the exception object about the source and nature of the exception if needed, or even catch specific exceptions if you want to customize the kind of information you want to give the user for a given exception.


Copyright 2001 Microsoft Corporation. All rights reserved.