Error handling in vb.net is implemented using the excetions instead of the On Error Goto syntax, and the On Error Goto is now not supported, and Visual Basic now provides a structured error-handling process that uses a try ... Catch... Finally expression. A structured error-handling process is related to exceptions, similar to a Select Case or a while, a code structure with a selection relationship. This makes the code of the program more robust and allows for more complex error handling. The operating mechanism of exceptions is such that, when there are errors, the error handling process is blocked, causing the error in the try module to run the code, and using the catch expression to get the error, the syntax is as follows:
First, run the code in the try module, run the code in the finally module if there is no error, run the code in the catch module if there is a mistake, and the user can make some error handling in the Catch module. Then run the code in the finally module and exit the entire module.
The following example shows how to make error handling settings, when the user accesses an array list, after the length of the list, an error occurs, the code is as follows:
The example above prints out the error message and indicates where the code has been wrong and continues to run. The following is the result of the run:
Conversely, if the user does not make error handling, it will get the wrong information from the system at run time, and exit the running environment directly, instead of continuing to run. The system will receive the following error message: