Can VB6 handle structured exceptions?

Source: Internet
Author: User

Can VB6 handle structured exceptions.

C ++ builder extends the processing of structured exceptions and supports the _ finally keyword to ensure that the routine has only one exit. It is a very scientific method to handle errors, it demonstrates the profound programming experience and wisdom of C ++ builder developers. VB. NET's imitation of it is the biggest praise for the handling of structural exceptions.
 
See an example of VB. NET structured exception handling.
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
Dim I as long, J as long
J = 0
Try
I = 1/J
Catch
Msgbox ("error ")
Finally
Msgbox ("finally ")
End try
End sub

Of course, in actual programming, we will not just pop up a boring msgbox action. Try Structure
We usually put some code that may cause exceptions. Most of this type of code accesses files (because the read/write files may be rejected, the disk is full, and so on), and accesses the Registry and other unreliable operations. When an exception occurs in code execution in the try structure, the program jumps directly to execute the code in the catch structure. In the catch structure, we usually put code that reports and handles errors, for example, msgbox is used to report to the user what is wrong, and how the next user should do so. No matter whether an exception occurs or not, the code in the finally structure is always executed. Therefore, we can put some statements called "cleanup code" in the finally structure, for example, close opened files, Close opened sentences, release object instances, and other operations.

VB6 is a com-based development language, which is very different from creating objects on the stack in C ++. The COM Object in VB6 has a reference counting mechanism, which means that after a routine ends, the object variable (pointer to the object instance) will be automatically destroyed, the reference counter value of the object instance is reduced by one. If the counter value is reduced to 0, the instance will be automatically destroyed by the system. The object created on the C ++ stack does not have this self-management mechanism. It requires the programmer to release the object instance explicitly. (For the principle of COM, let's talk about this first, and discuss it in detail later ). In short, for VB6, it is not so useful to use structured exceptions to ensure the destruction of object instances, but we have enough reason to use structured exception handling in VB6. Obviously, the situations mentioned above: Closing open files and closing open sentences are all reasons for using structured exception processing.

Although VB6 does not directly provide a structured exception handling mechanism, you only need to make some modifications to the on error statement.

See the structured exception handling simulated by VB6:
Try: On Error goto catch
'{

Add "unreliable code" here"

'}
Goto finally
Catch:
'{

Add "Error Report and error handling code" here"

'}
Finally:
'{

Add "cleanup code, such as closing files and closing sentences" here"

'}

Maybe you may disagree and tell me that the Code uses a GOTO statement, which is a bad programming style.
It is not recommended to use goto because misuse of goto will turn the code into a "mess", but it is obvious that this code does not
Abuse of Goto, but with a very careful use of Goto, all the Goto in the Code point to a uniform
Finally statement.

If you have "Goto Jie Li", you have to try this:
Try: On Error goto catch
'{

Add "unreliable code" here"

'}
Finally:
'{

Add "cleanup code, such as closing files and closing sentences" here"

Exit sub
'}

Catch:
'{

Add "Error Report and error handling code" here"

Resume finally
'}
This format is equivalent to the former, but it does not use Goto, but I think it is more "noodle" than the former. ^_^ I personally like the first one.

Postscript:
1. Note: The finally code must be solid. If an error occurs in finally, the exception cannot be handled.
2. this kind of simulation of structured exception processing is only a basic simulation. Compared with the real structured exception processing, there are still some differences. For example, the real structured exception processing can be nested, that is to say, a try .. catch... another try... can be embedded in the finally structure... catch... finally structure.
3. I do not reject or deny VB6's traditional error handling methods. VB6's error handling statements are powerful and can be used flexibly to better solve the problem. Do not limit them to one of the modes in this article. The traditional error handling methods are described in detail and examples in msdn.

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.