Udostępnij za pośrednictwem


How to: Catch an Exception in Visual Basic

This example shows how to use a Try and Catch block to catch exceptions.

Example

This example shows how to use a Try…Catch block to catch an OverflowException.

This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Visual Basic Language. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).

Dim Top As Double = 5
Dim Bottom As Double = 0
Dim Result As Integer 
Try
    Result = CType(Top / Bottom, Integer)
Catch Exc As System.OverflowException
    MsgBox("Attempt to divide by zero resulted in overflow")
End Try

This example requires:

  • A reference to the System namespace.

The following code example implements a Try...Catch block that handles Exception, IOException, and all the exceptions that derive from IOException.

Try 
    ' Add code for your I/O task here.  
Catch dirNotFound As System.IO.DirectoryNotFoundException
    Throw dirNotFound
Catch fileNotFound As System.IO.FileNotFoundException
    Throw fileNotFound
Catch pathTooLong As System.IO.PathTooLongException
    Throw pathTooLong
Catch ioEx As System.IO.IOException
    Throw ioEx
Catch security As System.Security.SecurityException
    Throw security
Catch ex As Exception
    Throw ex
Finally 
    ' Dispose of any resources you used or opened in the Try block.  
End Try

Add the code you want to execute to the Try block.

Robust Programming

Use this block of code as a starting point for wrapping a data operation in a Try...Catch statement. This Try...Catch block is designed to catch and rethrow all exceptions. That may not be the right choice for your project. For a discussion on exception-handling options, see Best Practices for Using IntelliSense Code Snippets.

You can reduce the likelihood of exceptions by using Windows Forms controls such as the OpenFileDialog Component (Windows Forms) component and the SaveFileDialog Component (Windows Forms) component controls that limit the user choices to valid file names. The FileInfo.Exists property can check whether a file exists before you try to open it. Using these controls and classes is not foolproof, however. The file system can change between the time the user selects a file and the time that the code runs. Exception handling will therefore nearly always be required when with working with files.

Security

For many file tasks, the assembly requires a privilege level granted by the FileIOPermission class. If you are running in a partial-trust context, the code might throw an exception due to insufficient privileges. For more information, see Code Access Security Basics. The user also needs access to the file, should it exist. For more information, see Access Control Lists (ACLs).

Do not make decisions about the contents of the file based on the file name extension. For example, the file Form1.vb may not be a Visual Basic source file.

See Also

Tasks

How to: Throw an Exception in Visual Basic

How to: Test Code with a Try…Catch Block in Visual Basic

How to: Clean up Resources with a Try…Finally Block in Visual Basic

How to: Filter Errors in a Catch Block in Visual Basic

How to: Check an Exception's Inner Exception

Troubleshooting Exception Handling

Concepts

Choosing When to Use Structured and Unstructured Exception Handling