VB.net captures and throws exceptions

Source: Internet
Author: User

After learning VB.net for a few days, I don't feel much about it. I want to summarize my studies over the past few days, but I don't know what I learned, however, I still have the confidence to learn it well. After reading the notes in onetnote over the past few days, I found that the capture and throw exceptions should be summarized. Although I have not summarized a lot of knowledge, I would like to summarize it, my heart is clear. Come on!

Structured exception handling follows . Net The first version Visual Basic Language. Structured Exception Handling
Compared with the previous On errorgoto... Statement, you can use a syntax that is more suitable for other parts of the languageCodeMore readable , Also Compared with the previous On
Error goto... Statements are more powerful and provide more flexible control capabilities.

 

 Capture and throw exceptions

 

Exception Handling is usedTry... catch... finally... end tryStatement implementation. The basic syntax format is as follows:

 

Try

'Code that can cause an exception

Catch

'Code for handling exceptions

Finally

'Code that implements cleanup (for example, shutting down database connections, and so on)

End try

Resolution:

TryAndEnd tryStatements are required.CatchAndFinallyStatement can be used together inTryBlock, but at least one of them is used. In addition, you can use multipleCatchStatement to handle different types of exceptions. If you have multipleCatchBlock, which can be sorted:

From the most specific exception type to the most rough exception type:

Try

'Code that can cause an exception

Catch ex asargumentoutofrangeexception

'It is possible to use a default value to process an invalid parameter so that the code can continue to be executed.

Catch ex as exception

'Handle any other types of exceptions 

Finally

'Code that implements cleanup (for example, shutting down database connections, and so on)

End try

Resolution:

You can also throw an exception in your code. It is useful to capture and throw exceptions when you execute some cleanup operations in the code.-In this way, a higher-level process can capture it. It is also useful to throw an exception when you create a custom exception type. 

To throw an exception, you can write the following code:

Throw newargumentoutofrangeexception

 

Resolution:

argumentoutofrangeexception tell throw type of exception to be thrown by the statement. argumentoutofrangeexception type is only . net one of the many types provided by the framework.

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.