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
setjmp
And
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,
- Function A calls the
setjmp
set of a jump bit, and function A calls function B.
- 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 + +
setjmp
And
longjmp
The 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++?