In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use VB.NET structured exception handling, the article introduces in great detail, has a certain reference value, interested friends must read it!
Overview
Compared with VB language, VB.NET introduces many features. One of the most noteworthy changes is the introduction of VB.NET structured exception handling. Although VB.NET still supports exception handling of type On Error Goto, this is not very good. We should take full advantage of the structured exception handling provided by VB.NET.
Proper error handling is important
Any application needs an appropriate exception handling. However, due to strict development time constraints, shared responsibilities, and conflicting priorities, developers often overlook the importance of organized and thoughtful exception handling. This often leads to inconvenience and failure for users, and * has to add a lot of code rewriting work.
Structured exception handling
VB.NET uses the standard error reporting mechanism of the .NET Framework architecture, known as structured exception handling; it reports errors in medical programs based on exceptions. Exceptions are classes that can catch error messages. In order to properly use the exception handling mechanism of .NET, developers need to write clever code to closely observe exceptions, as well as write execution code to handle them.
VB.NET structured exception handling provides the following sections in the code:
Try snippet: a code snippet that can generate an exception and can always be executed
Catch snippet: a code snippet that attempts to handle an exception and is executed when the exception occurs
Finally snippet: a code snippet that does some cleanup and can always be executed
Abnormal class
Every exception class in .NET is derived from the System.Exception class. The most frequently used members of the exception class are as follows:
Message: describe the specific information of an error
Source: the name of the object or application that caused this exception
Goal: ways to handle exceptions
Try...Catch block
The purpose of the Try...Catch block is to allow errors to be caught and to specify a specific VB.NET structured exception handling solution for them. The code looks like this:
Try 'executed code Catch' error resolution code Catch ends
Use Try snippets to write the code to be executed, and Catch snippets to catch errors that may occur when executing Try snippets of code and react accordingly. The protection code that appears in the Try section is always executed; however, the code in the Catch section is executed only when an error occurs. The Try segment of the code is always executed.
Try...Catch...Finally block
The purpose of using the Try...Catch...Finally block is to allow the protected code in the Try section to be executed, to react to any possible errors in the Catch block, and to have cleanup code in the subsequent Finally block. The code in the Try block is executed regardless of whether there is an error in the Finally block. In this way, it is convenient to ensure that the allocated resources will be released, and it can easily provide functions that need to be performed regardless of the error control details. The code example is as follows:
Try 'execute Code Catch' error Resolution Code Finally 'Clean Code Catch end
Catch all exceptions and concrete classes of exceptions
VB.NET structured exception handling is flexible, allowing you to catch a specific type of exception or any type of exception, depending on how we use it.
Example: catch any possible exception
Try Dim i As Integer = 0 Dim iresult As Integer iresult = 1 / I
Using ex as an exception catch
MessageBox.Show
(ex.ToString ())
Finally
MessageBox.Show
("finally block executed")
End of Try
How does this work?
In the above VB.NET structured exception handling code example, we deliberately set a run error to prove that any exception can be caught. We catch any error that occurs and react to it, regardless of the type of error. The error occurs in the Try code block, so when the exception occurs, the Catch code block is then executed, followed by the Finally code block. We catch this exception by declaring a variable of exception type ex.
Example: catch a specific type of exception
Try Dim i As Integer = 0 Dim iresult As Integer iresult = 1 / I
Catch ex as an exception of the overflow type
MessageBox.Show
(ex.ToString ())
Finally
MessageBox.Show
("finally block executed")
End of Try
How does this work?
The second code example causes the same error because it attempts to perform a division by 0, which causes an overflow. However, in this example, we are only interested in catching exceptions of this type, so the type of ex variable is specifically defined as the OverflowException type. The code result of running the second example is the same as running the * example, because the same error is caught in both examples; however, the second example cannot catch other types of exceptions (exceptions of non-overflow types). But as before, the code in the Finally block is executed regardless of whether an exception is caught or not.
The above is all the content of the article "how to use VB.NET structured exception handling". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.