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.