My opinion of C/C + + Why use exceptions in--c++?

Source: Internet
Author: User
Tags goto

Why do I use exceptions in C + +?

Many people may know about the anomaly mechanism in C + +, which many people may not know. Many people know what is commonly used in C assert , and know that it is specified at compile time NODEBUG to ignore it.

For the C language, using normal if-else is a good choice, whereas in C + +, if you use object-oriented programming, it is best to use the Exception mechanism. The main design object can be the correct problem of the destruction.

Error in C jump setjmpAnd longjmp

A common method for handling exceptions in C language. It does not abort assert exit directly like or or just like that exit , nor does it have to be goto confined to the inside of a function.
It is used for a kind of long jump way. You can jump from one function to the calling function in the upper layer of the function.
As an example,

    1. Function A calls the setjmp set of a jump bit, and function A calls function B.
    2. Called in function B, it causes the longjmp program bar to continue executing where it is called in function a setjmp .

This is not the focus of this article.

The use setjmp and longjmp maximum disadvantage is that the construction or destruction of certain objects may be skipped.
Also, in C, goto you can skip the definition of certain variables, but there's nothing wrong with that. You can try the following code. Note that is the C language, if you use C++ the compiler to compile, it should be an error.

1#include <stdio.h>2 3 intMainintargcChar**argv)4 {5     if(Argc >1){6         GotoNodef;7     }8     intA =102;9 Nodef:Tenprintf"A =%d\n", a); One     return 0; A}

Using in C + + setjmpAnd longjmpThe adverse consequences of

Let's look at the code first

The code for the object could not be properly destructor
1#include <iostream>2#include <csetjmp>3 4 usingstd::cout;5 usingStd::endl;6 7 classtest{8      Public:9Test () {cout<<"Test construct"<<Endl;}Ten~test () {cout<<"Test destructor"<<Endl;} One }; A  -Jmp_buf Jbuf;//for setjmp Saving current related information -  the voidcalljmp () - { -Test T;//test to correctly invoke the destructor -cout<<"Call longjmp (jbuf,3721)"<<Endl; +LONGJMP (Jbuf,3721); - } +  A intMain () at { -     intret=0; -     if(0= = (ret=setjmp (JBUF))) { -cout<<"Call setjmp (jbuf) resuces"<<Endl; - calljmp (); -     } in     Else{ -cout<<"Call setjmp (jbuf) failed ret ="<< ret <<Endl; to     } +}

Compile and execute to see

As you can see, the object is constructed, but there is no normal invocation of the destructor.

1 [email protected]:~/code_/exception$ g++ setjmp. cpp -o setjmp2 [email protected]:~/code_/exception$./3callsetjmp ( JBUF) resuces4Test construction 5 call longjmp (Jbuf,3721)6 Call setjmp (JBUF) failed   3721

Cases where exception handling is used in C + +

One of the benefits of using exception mechanisms in C + + is the ability to properly deconstruct objects.

Code that uses the exception handling mechanism
1#include <iostream>2#include <csetjmp>3 4 usingstd::cout;5 usingStd::endl;6 7 classtest{8      Public:9Test () {cout<<"Test construct"<<Endl;}Ten~test () {cout<<"Test destructor"<<Endl;} One }; A  -Jmp_buf Jbuf;//for setjmp Saving current related information -  the voidcalljmp () - { -Test T;//test to correctly invoke the destructor -cout<<"Call longjmp (jbuf,3721)"<<Endl; +     //longjmp (jbuf,3721); -     Throw 3721; + } A  at intMain () - { -     Try{ -cout<<"call calljmp attempt to throw an exception"<<Endl; - calljmp (); -}Catch(intt) { incout<<"to catch an exception value:"<<t<<Endl; -     } to     /* + int ret=0; - if (0 = = (ret=setjmp (jbuf))) { the cout<< "Call setjmp (JBUF) resuces" <<endl; * calljmp (); $     }Panax Notoginseng else{ - cout<< "Call setjmp (JBUF) failed ret =" << ret <<endl; the     } +     */ A}

Compile run try

You can see that the destructor is called normally this time.

[Email protected]:~/code_/exception$ g++ exception. cpp -o exception[email protected]-pc:~/code_/exception$./Exception Call calljmp attempt to throw an exception test constructs the calling LONGJMP (Jbuf,3721) Test destructor captures the exception value:3721

My opinion of C/C + + Why use exceptions in--c++?

Related Article

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.