Udostępnij za pośrednictwem


How to: Implement I/O Try...Catch Blocks in Visual Basic

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

Example

This example moves from the most specific to the least specific; each exception is tested in turn.

This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in File system - Processing Drives, Folders, and Files. For more information, see How to: Insert IntelliSense Code Snippets.

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

Compiling the Code

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 ACL Technology Overview.

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: Filter Errors in a Catch Block in Visual Basic

How to: Check an Exception's Inner Exception (Visual Basic)

Reference

Try...Catch...Finally Statement (Visual Basic)

Concepts

Best Practices for Using IntelliSense Code Snippets

Structured Exception Handling Overview for Visual Basic

Choosing When to Use Structured and Unstructured Exception Handling (Visual Basic)

Other Resources

Exception Handling Tasks (Visual Basic)