Vb. NET Error exception handling

Source: Internet
Author: User
Tags finally block

relative to VB terms, vb.net has introduced a lot of features. What attracts me most is the introduction of structured exception handling.

Although vb.net still supports exception handling on the Error Goto type, this is not very good. In contrast, structured exception handling is more flexible and easier to use. We should take full advantage of the structured exception handling provided by vb.net.

in the vb.net exception Handling in the structure is divided into 3 block of statements.

( 1 ) the try block is responsible for capturing the error code

( 2 ) catch is handled incorrectly

( 3 ) Finally, the job that is responsible for error handling, such as releasing objects, cleaning up resources, and so on.

The purpose of using these blocks is to agree to executeTrycode that is protected in the module, theCatch, theCatchThere may be no errors in the block to respond. And in the ensuingFinallyThere is also cleanup code in the block. No matterTryIf there is an error in the code block,Finallythe code of the block is executed so that it is very convenient to ensure that the allocated resources are freed, and that it is convenient to provide functions that need to be executed regardless of the error control details. But,The catch statement block does not have to be executed, assuming that the code inside the try block is not an error and no exception is thrown, the code in the Catch statement block is not executed. Instead, the catch block is skipped to perform cleanup work directly in the finally block.

Conversely, it is assumed that the processing in the exception catch statement block will be performed.

Here are some examples:

   Private Sub button1_click (sender as Object, e as EventArgs) Handles Button1.Click        Dim file as System.IO.FileStream, MS G as String        Try            file = New System.IO.FileStream ("D:\readme.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read)            file. Close ()        Catch ex as Exception            msg = String.Format ("{0}ralsed Exception: {1}", ex. Source, ex. Message)            MessageBox.Show (msg, "My App", MessageBoxButtons.OK, Messageboxicon.error)        Finally            If not file Is nothing and then file. Close ()        End Try    End Sub
This example is in D Drive to find a readme.txt document. Assuming we do not have this document set up in the D Drive, we run a Catch statement that pops up the source and cause of the error:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmduzotm1mg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

Let's say we're D disk to create a Readm E.txt documentation. Then there will be no error, and the Catch statement will not run . Jump to Finally run.

Today, developers often overlook the organization. The importance of thoughtful exception handling often leads to inconvenience and failure of the user.

In the future we assume that to be a qualified developer, exception handling is critical to us.

It is an important security mechanism of the. NET platform, which separates the acceptance and processing of error codes perfectly. Clearing the programmer's mind also helps the code enhance readability, facilitates reading and understanding of the maintainer, and provides methods for handling the execution of any unexpected or unusual situation.



Vb. NET Error exception handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.