Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How VB.NET uses On Error statements

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces VB.NET how to use On Error sentence, the article is very detailed, has a certain reference value, interested friends must read it!

VB.NET recommends using Try...End Try blocks for structured exception handling, but it also borrows the VB.NET On Error statement from previous versions of BASIC to ensure compatibility. In fact, On Error is not an advantage of VB, because using it will destroy the structure of the program and make it difficult for programs with exception handling to understand and debug. But I've always been amazed at how VB engineers implement it, because On Error makes abnormal jumps flexible, not as limited as Try. First, take a look at how Try is implemented:

Public Function F1 () As Integer Try Dim n As Integer = 2\ n Catch ex As Exception MsgBox (ex.Message) End Try End Function

This is the simplest exception handler, and through Reflector disassembly (if you use ILDasm, do not select "expand try-catch"), you can find that the whole process has been translated into 19 instructions. Pay attention to this sentence:

.try L_0000 to L_0006 catch Exception L_0006 to L_0022

This is a typical try block, specifying the exception to catch directly at catch, and then specifying the location of the catch area, which is very clear. Also pay attention to these two sentences:

L_0007: call ProjectData.SetProjectError L_001b: call ProjectData.ClearProjectError

As you can see, these two sentences are at the beginning and end of the catch block. Delving into these two processes, I found that it was logging exceptions for the Err object. It seems that the use of Err is also a grammatical sweetness, performance is difficult, adding these two sentences out of thin air (fortunately, they are not too complicated).

Next I wrote a function similar to this, using the On statement to handle exceptions:

Public Function F2 () As Integer On Error GoTo CATCHBLOCK Dim n As Integer = 2\ n Exit Function CATCHBLOCK: MsgBox (Err.Description) End Function

This is no more complicated than the previous process, but after disassembly, its IL code has 47 instructions, only 19 just now! The main change is the try part, which now looks like this:

.try L_0000 to L_0022 filter L_0022 L_0036 to L_0060

Notice that catch is gone and filter appears. I have never seen filter in the IL generated by C #. I queried the documentation in the Meta Data section, and filter can probably do some filtering to meet certain conditions before entering the block that handles the exception. In this example, the Line0022 instruction starts as a filter, which is:

L_0022: isinst Exception L_0027: brfalse.s L_0033 L_0029: ldloc.s V_4 L_002b: brfalse.s L_0033 L_002d: ldloc.3 L_002e: brtrue.s L_0033 L_0030: ldc.i4.1 L_0031: br.s L_0034 L_0033: ldc.i4.0 L_0034: endfilter

Endfilter is the beginning of the exception handling part of the code. The code before L0030 is the judging part of the filter, and Vip4 and Vip3 are the variables that VB adds to save the error code. In the whole disassembly, I found that the code designed to handle the exception part is also in the try block in IL, that is to say, the structure of the program is no longer a regular try...catch block, and the statement that produces the exception is with the statement that handles the exception, while the instruction that really handles the exception is a lot of cumbersome and procrastinating jump statements.

Let's take a look at my third example:

Public Function F3 () As Integer On Error Resume Next Dim n As Integer = 2\ n End Function

The two-line process uses VB's powerful syntax killer, On Error Resume Next, which ignores all exceptions and allows the code to continue to execute immediately after the statement that produced the exception. Guess how many IL instructions this function produces? The answer is 50! Longer than a normal On Error. I won't say much about its implementation, which is similar to the previous On statement. However, the number 50 seems to remind you that do not lazily use On Error to handle exceptions in your program, and the cost is unacceptable.

* an example is VB.NET 's When statement, which can filter the Catch part:

Public Function F1 () As Integer Dim n As Integer = 0 Try Dim m As Integer = 2\ n Catch ex As Exception When n = 0 MsgBox (ex.Message) End Try End Function

The When statement in it determines the variable n and enters the processing section only when n = 0. Hearing the word "filter", we have guessed that it is implemented in try...filter. That's right. The filter here is mainly about whether ex is exception, whether n is equal to zero, and so on. When filtering is successful, it will be transferred to the exception handling section for processing. This time the code generated by VB is much more regular than the VB.NET On Error statement, and the structure is quite clear.

The above is all the content of the article "how to use On Error sentences in VB.NET". 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report