C language uses setjmp/longjmp to simulate the hierarchical Exception Handling Mechanism of C ++

Source: Internet
Author: User

Traditional C language Exception Handling Mechanism


In traditional C language Exception Handling, generally, by setting different return values or setting a global variable value indicating an error, the caller may be able to execute normally or encounter various errors, exceptions, warnings, and so on. The caller determines whether an exception has occurred and processes the exception based on the returned value of the caller. The following is a simple example of this processing method:
[Cpp]
Fun1 ()
{
....
Int result = fun2 ();
Switch (result)
{
Case 1 ....
Case 2 ....
....
}
....
}
 
Fun2 ()
{
....
... Return-1;
....
... Return 1;
....
}
More Reasonable Exception Handling Mechanism

 

In the traditional C language exception handling mechanism, if an exception needs to be transmitted by level, the return values of each lower-layer interface should be determined at each layer, and corresponding error handling code should be written. Errors that cannot be processed at this layer must be uploaded to the top-layer interface for processing. In such a layer-by-layer call, when there are many layers, errors will be passed up layer by layer, and a lot of code needs to be written, causing a lot of trouble to the programmer and easy to make mistakes. In addition, a large number of error handling codes can reduce the readability of the main interface processes. Despite continuous checks and debugging, an error is a small probability event, but it needs to be determined at each layer, affecting efficiency.

Therefore, we need to reflect on the exception handling mechanism we need in large-scale C Projects? For example, the try/catch/throw Exception Handling Mechanism in JAVA and C ++ is more effective. When an error occurs, you can immediately jump to the high-level interface capable of handling the error without passing the exception layer by layer.

 

Implementation Details

In C, we can use setjmp/longjmp to implement a mechanism similar to C ++ exception handling.

Their prototype is: int setjmp (jmp_buf env );

Void longjmp (jmp_buf env, int val );
To use setjmp/longjmp, you must add the header file setjmp. h to the program. Setjmp () is used to save the content in the system stack in the buffer zone envbuf. The returned value of this macro is 0. Longjmp () redirects the program to the last time setjmp () is called, and then the following statement is re-executed. However, the second parameter of longjmp () passes a value to setjmp (). The value is the return value of setjmp () after longjmp () is called, in this way, different return values are used to distinguish the first call of setjmp.

It should be noted that setjmp/longjmp can be used across functions, so they can be used to implement an exception handling mechanism at the cross-function call level.

The following is a simple example:

We have three small files, main. c file1.c file2.c, which are listed below:

[Cpp]
Int main ()
{
Jmp_buf main_stat;
If (setjmp (error_stat )! = 0)
{
Printf ("error in main \ n ");
Return-1;
}
File1 ();
Return 0;

}

[Cpp]
Int file1 ()
{
Jmp_buf file1_stat;
Memcpy (& file1_stat, & error_stat, sizeof (file1_stat ));
If (setjmp (error_stat )! = 0)
{
Printf ("error in file1 \ n ");
Memcpy (& error_stat, & file1_stat, sizeof (file1_stat ));
Longjmp (error_stat, 1 );
}
File2 ();

}

[Cpp]
Int file2 ()
{
Jmp_buf file2_stat;
Memcpy (& file2_stat, & error_stat, sizeof (file2_stat ));
If (setjmp (error_stat )! = 0)
{
Printf ("error in file2 \ n ");
Memcpy (& error_stat, & file2_stat, sizeof (file2_stat ));
Longjmp (error_stat, 1 );
}
Printf ("start error \ n ");
Longjmp (error_stat, 1 );

}

Compile and run them. The output result is:
 

Start error
Error in file2
Error in file1
Error in main

This is just a simple example, and the code is passed by hierarchy, and the code is not passed by hierarchy.

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.