Before introducing how the C ++ compiler implements Exception Handling, let's take a look at the C ++ compiler? In fact, the C ++ compiler is a highly compatible compiling environment with standardized C ++. Different compilers will also perform different optimizations on different CPUs.
This article discusses how the C ++ compiler can handle exceptions. I will assume that you are familiar with the exception handling syntax and mechanism, used for the exception handling library of VC ++, and replaced the one provided by VC ++ with the handler in the library, you only need to call the following function:
- install_my_handler();
After that, all exceptions in the program, from being thrown to the stack to expand the stack unwinding), then to calling the catch Block, and finally to the normal operation of the program, will be managed by my Exception Handling library.
Like other C ++ features, the C ++ standard does not specify how the compiler can handle exceptions. This means that every compiler provider can implement it in what they think is appropriate. Next I will describe how VC ++ works, but even if you use another compiler or operating system ①, this article should be a good learning material. The implementation method of VC ++ is based on the construction of the windows system Exception Handling SEH) ②.
In my opinion, C ++ compiler exceptions are thrown explicitly, or caused by division by zero overflow and NULL pointer access. When it occurs, an interruption occurs, and the control is passed to the operating system. The operating system will call the exception handling program, check the function call Sequence starting from the exception location, and expand the stack and transfer control. Windows defines the structure "EXCEPTION_REGISTRATION", which enables us to register our own exception handling program with the operating system.
- struct EXCEPTION_REGISTRATION
- {
- EXCEPTION_REGISTRATION* prev;
- DWORD handler;
- };
During registration, you only need to create such a structure, and then place its address at the position of the FS segment offset 0. The following Assembly Code demonstrates this operation:
- mov FS:[0], exc_regp
The prev field is used to create a chain table of the prediction_registration structure. Every time we register a new prediction_registration, We need to store the previously registered address in prev. So what is the length of the exception callback function? In excpt. h, windows defines its prototype:
- EXCEPTION_DISPOSITION (*handler)(
- _EXCEPTION_RECORD *ExcRecord,
- void* EstablisherFrame,
- _CONTEXT *ContextRecord,
- void* DispatcherContext);
Let's take a look at a simple example. The following program registers a C ++ compiler exception handler and generates an exception by dividing by zero. The exception handler captured it, printed a message, and exited.
- # Include
- # Include
-
- Using std: cout;
- Using std: endl;
-
- Struct prediction_registration
- {
- Prediction_registration * prev;
- DWORD handler;
- };
-
- Prediction_disposition myHandler (
- _ EXCEPTION_RECORD * ExcRecord,
- Void * EstablisherFrame,
- _ CONTEXT * ContextRecord,
- Void * DispatcherContext)
- {
- Cout<<"In the exception handler"<< Endl;
- Cout<<"Just a demo. exiting ..."<< Endl;
- Exit (0 );
- Return ExceptionContinueExecution; // It will not run to this
- }
-
- IntG_div=0;
-
- Void bar ()
- {
- // Initialize a prediction_registration Structure
- Prediction_registration reg ,*Preg=®
- Reg. handler= (DWORD) myHandler;
-
- // Obtain the "Header" of the current Exception Handling Link"
- DWORD prev;
- _ Asm
- {
- Mov EAX, FS: [0]
- Mov prev, EAX
- }
- Reg. prev= (Prediction_registration *) prev;
-
- // Register!
- _ Asm
- {
- Mov EAX, preg
- Mov FS: [0], EAX
- }
-
- // Generate an exception
- IntJ=10/G_div; // exception, Division by zero Overflow
- }
-
- Int main ()
- {
- Bar ();
- Return 0;
- }
Note: prediction_registration must be defined on the stack and must be located at a memory address lower than the previous node. If windows has strict requirements on this, the process will be terminated immediately if it fails to meet these requirements.
- How to Write C ++ project development and project plan correctly
- Summary Notes on learning and exploring C ++ library functions
- In-depth demonstration of high security of C ++
- Describes in detail how to accurately Write C ++ languages.
- In-depth demonstration of high security of C ++