The statement between the Try � Catch keywords is called protected code because any runtime errors resulting from these statements won�t cause the program to crash.
Example:
Try
prcTextFile.StartInfo.FileName = ("C:\Program Files\ErrorHandler\VbNetError.txt ")
prcTextFile.Start ()
Catch
MessageBox.Show ("Unable to locate the desired file")
Finally
MessageBox.Show ("Error Handler Complete")
End Try
In a real program you�ll probably want to use the Finally code block to update important variables or properties, display data, or perform other cleanup operations.
5.More Complex Try --- Catch Error Handlers:
As your program become more sophisticated, you might find it useful to write more complex Try --_ Catch error handlers that manage a variety of runtime errors and unusual error handling situations.Try --- Catch provides for this complexity by:
(1). Permitting multiple lines of code in each Try, Catch or Finally code block.
(2). Offering the Catch When syntax, which tests specific error conditions.
(3). Allowing nested Try --- Catch code block, which can be used to build sophisticated and robust error handlers.
In addition a special error handling object named Err allows you to identify and process runtime errors and conditions in your program.
6.The Err Object:
Err is a special Visual Basic object that�s assigned detailed error handling information each time a runtime error occurs. The most useful Err properties for identifying runtime errors are Err.Number and Err.Description.
Err.Number: contains the number of the most recent runtime error.
Err.Description: a short error message that matches the runtime error numbers.
By using the Err.Number and Err.Description properties together in an error handler, you can recognize specific errors and respond to them and you can give the user helpful information about how he or she should respond.
The Following table lists many of the runtime errors that Visual Basic application can encounter:
Error Number |
Default Error Message |
5 |
Procedure call or argument is not valid |
6 |
Overflow |
7 |
Out of Memory |
11 |
Division by Zero |
51 |
Internal Error |
52 |
Bad file name or number |
53 |
File not found |
55 |
File already open |
76 |
Path not found |
482 |
Printer error |
For more information on a particular error, search for Visual Studio online Help. Unused error numbers in the range of 1-1000 are reserved for future use by Visual Basic.NET.
7.Test for Multiple Runtime Errors:
Try
prcTextFile.StartInfo.FileName = ("C:\Program Files\ErrorHandler\VbNetError.txt ")
prcTextFile.Start ()
Catch When Err. Number = 53 'If File Not Found Error
MessageBox.Show ("Unable to locate the desired file")
Catch When Err. Number = 7 'If Out of Memory Error
MessageBox.Show ("Are you sure about the file Path")
Catch
MessageBox.Show ("Problem Loading File")
Finally
MessageBox.Show ("Error Handler Complete")
End Try
8.Exit Try:
As with any block structure, it is very nice to be able to jump out of the structure when needed. This is the purpose of the Exit Try statement.
If we have a Finally block that block�s code must be run before the Try block is exited.
Try
'Any Protected Code
prcTextFile.StartInfo.FileName = ("C:\Program Files\ErrorHandler\VbNetError.txt ")
prcTextFile.Start ()
If blnFlg = True Then Exit Try
Catch
'Error Handling Logic/Code
MessageBox.Show ("Unable to locate the desired file")
Finally
'Execution Resumes Here
MessageBox.Show ("Error Handler Complete")
End Try
Based on the flow of your code, you can jump out of the protected region at any point with the Exit Try statement.
9.Error Handlers and Defensive Programming Techniques:
Consider the following piece of code:
If File exists Then
prcTextFile.StartInfo.FileName = ("C:\Program Files\ErrorHandler\VbNetError.txt ")
prcTextFile.Start ()
Else
MessageBox.Show ("File Not Found")
End If
This If---Then statement isn�t an actual error handler because it doesn�t prevent a runtime error from halting a program. Instead, it�s a validation technique that some programmers call Defensive Programming.
In this particular case, testing to see whether a file exists with a .NET framework method is actually faster than waiting for Visual Basic to issue an exception and recover from an runtime error using an error handler. When should you use defensive programming techniques and when should you use error handlers? The answer depends on how often you think a problem will occur with the statements you plan to use. If there�s a real likelihood that a piece of code will produce a runtime error more than 25 percent time, defensive programming logic is usually the most efficient way to manage potential problems.
10. Conclusion:
Thus Visual Basic .NET offers very flexible error handling technique. I hope this article guides beginners about implementing error handling in their application.