The C ++ compiler is a highly compatible compiling environment with standardized C ++. Its powerful CPU optimization function can make the program very clean and easy to maintain, now let's move on to the C ++ compiler world with this article.
This article discusses how the C ++ compiler can handle exceptions. I will assume that you are familiar with the syntax and Mechanism of exception handling. This article also provides an exception handling library for VC ++. Replace the one provided by VC ++ with the handler in the library. You only need to call the following function:
- struct EXCEPTION_REGISTRATION
- {
- EXCEPTION_REGISTRATION* prev;
- DWORD 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, the exception handling library is used to manage exceptions. Like other C ++ features, the C ++ standard does not specify how the compiler can handle exceptions.
This means that every C ++ compiler provider can implement it in what they think is appropriate. Next I will describe how VC ++ works, but even if you use another C ++ compiler or operating system ①, this article should be a good learning material.
The implementation method of VC ++ is based on the structured exception processing SEH of the windows system) ②. Structured exception handling-Summary In this article, I think exceptions are thrown explicitly, or caused by division by zero overflow, null pointer access, and so on.
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.
- # 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
- }
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.
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code