In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "case analysis of exception handling mechanism in C language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "C language exception handling mechanism case analysis" it!
Exception handling mechanism: setjmp () function and longjmp () function
The C standard library provides two special functions: setjmp () and longjmp (), which are the basis of structured exceptions and use the characteristics of these two functions to implement exceptions.
Therefore, the handling of exceptions can be described as follows:
First set a jump point (the setjmp () function can do this), and then call longjmp () anywhere in the subsequent code to jump back to the jump point, so that when an exception occurs, you go to the program that handles the exception, which will be described in the following introduction.
Setjmp () returns to the save site for the jump and provides a handler for the exception, longjmp () jumps (throwing an exception), and setjmp () and longjmp () can jump between functions, just like a global goto statement that can jump across functions.
For example, if the program uses setjmp () to set the jump within the main () function and calls another function A, and calls BMageB within function A to throw an exception (calling the longjmp () function), the program jumps directly back to where setjmp () is used in the main () function and returns a value.
-
Jmp_buf abnormal structure
Before using the setjmp () and longjmp () functions, you need to understand the jmp_buf exception structure. Jmp_buf will be used in the setjmp () function to save the current program site (to save the values of the registers currently needed), and the jmp_buf structure is declared in the setjmp.h file:
Typedef struct {unsigned jackers sp; / / stack pointer register unsigned jackers; / / stack segment unsigned jackers; / / flag register unsigned jackers; / / code segment unsigned jackers; / / instruction pointer registers unsigned jackers; / / base address pointers unsigned jackers; / / destination pointers unsigned jackers; / / additional segment unsigned jackers; / / source indexed unsigned j_ds / / data segment} jmp_buf
The jmp_buf structure stores the value of the program's current register to ensure that you can jump back to that execution point to continue execution after using longjmp ().
-
Setjmp () and longjmp () functions are described in detail
The prototypes of setjmp () and longjmp () functions are as follows:
Void _ Cdecl longjmp (jmp_buf jmpb, int retval)
Int _ Cdecl setjmp (jmp_buf jmpb)
_ Cdecl declares that the parameters of the function are stacked in the standard C way (from right to left). _ Cdecl is a calling convention of the C language, and PASCAL is also one of the calling conventions. The function declared by the C standard calling convention (_ Cdecl) does not automatically clear the stack, which is the responsibility of the caller-- which is why C can support an unfixed number of arguments. In addition, this calling convention adds an underscore character before the function name, such as a function declared as:
Int cdecl DoSomething (void)
The DoSomething is automatically prefixed with an underscore at compile time, that is, the function name becomes: _ DoSomething.
Both the setjmp () and longjmp () functions use the jmp_buf structure as a formal parameter, and their calling relationship is as follows:
First, call the setjmp () function to initialize the jmp_buf structure variable jmpb, store most of the registers in the current CPU that affect the execution of the program into jmpb, and provide a jump for the longjmp () function. The setjmp () function is an interesting function that can return twice. It should be the only one of all library functions that can return twice. The first time is initialization, returning zero. After the second call to the longjmp () function, the longjmp () function causes the setjmp () function to return a second time, and the return value is given by the second argument of longjmp () (integer, which should not return zero again).
After initializing jmpb with setjmp (), you can use the longjmp () function anywhere in the subsequent program to jump to the position of the setjmp () function. The first parameter of longjmp () is the jmpb initialized by setjmp (). If you want to jump back to the setjmp () just set, the first parameter of the longjmp () function is the exception jmpb initialized by setjmp (), which also shows one thing, that is, the exception jmpb. It generally needs to be defined as a global variable, otherwise, if it is a local variable, it is almost impossible to use when cross-function calls are made (unless you pass jmpb as an argument every time a function call is encountered, but obviously it is not worth doing so) The second argument to the longjmp () function is the second return value passed to setjmp (), which was introduced in the introduction to the setjmp () function.
Exception handling process
First compare (refer to) C++ 's exception handling, C++ adds an exception handling mechanism in the language layer, using try blocks to contain those code that may have errors, you can throw exceptions in try block code, C++ uses throw to throw exceptions. After an exception is thrown, it is transferred to the exception handler for execution. C++ uses catch blocks to contain code that handles exceptions, and catch blocks can receive different types of exceptions. It should be noted that throw generally does not throw an exception in the code in the try block. The code in the try block calls other functions, such as function A, and function An also calls the function BFinance. You can throw an exception in function B, or a deeper function call layer. In any case, as long as an exception is thrown, the program will be transferred to catch for execution.
How to implement, or specifically simulate, this function in C #?
Here are some simple methods.
Now assume that the second value of longjmp () is 1, which means that setjmp () will return 1 the second time. We use a simple set of macros instead of setjmp () and longjmp () to use:
First define a global exception:
Jmp_buf Jump_Buffer
Because setjmp () returns 0 after the first call to initialization and non-0 the second time, you can define a macro to make it function similar to C++ 's try.
# define try if (! setjmp (Jump_Buffer))
When the setjmp () function is not true for the first time, the code within the try block is executed, such as:
Try {
Test ()
}
When setjmp () returns a second time because of a call to longjmp () that throws an exception (the program will go to the setjmp () function to return), the exception handling code should be executed. Longjmp () causes the setjmp () function to return a non-zero setjmp if (! setjmp (JumpBuffer)). Therefore, exception handling should be followed by an else:
# define catch else
In this way, it looks similar to C++. The second return of the setjmp () function causes the expression in if () to be false, just so that the catch block can be executed, such as:
Try {
Test ()
} catch {
Puts ("Error")
}
Implementing a throw statement such as C++ actually replaces the call to longjmp (jmp_buf, int) with a macro:
# define throw longjmp (Jump_Buffer, 1)
The following routine explains how to use these macros:
-
# include "stdio.h" # include "conio.h" # include "setjmp.h" jmp_buf Jump_Buffer;#define try if (! setjmp (Jump_Buffer)) # define catch else#define throw longjmp (Jump_Buffer,1) int Test (int T) {if (T > 100) throw; else puts ("OK."); return 0;} int Test_T (int T) {Test (T); return 0;} int main () {int T Try {puts ("Input a value:"); scanf ("% d", & T); Troubles; Test_T (T);} catch {puts ("Input Error!");} getch (); return 0;} so far, I believe you have a deeper understanding of "case Analysis of exception handling Mechanism in C language". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.