Because the C ++ compiler has too many programs and cannot be uploaded here, you can obtain them from communitysever and decompile them for your own use. If not, search for them on the network, there are many resources! The following is a detailed description.
It is an excellent alternative solution. It clearly separates normal code and error handling code, and the program becomes very clean and easy to maintain. This article discusses how the compiler implements exception handling. I will assume that you are familiar with the syntax and Mechanism processed by the C ++ compiler. 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, 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) ②.
- struct EXCEPTION_REGISTRATION
- {
- EXCEPTION_REGISTRATION* prev;
- DWORD handler;
- };
In this article, I think 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 calls the exception handling program, and the function call Sequence starting from the location where the C ++ compiler exception occurs to 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
- }
-
- // Generate an exception
- IntJ=10/G_div; // exception, Division by zero Overflow
- }
-
- Int main ()
- {
- Bar ();
- Return 0;
- }
-
- /* ------- Output -------------------
- In the exception handler
- Just a demo. exiting...
The C ++ compiler is used to create a linked list 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: note that prediction_registration must be defined on the stack and must be located at a memory address lower than the previous node. windows has strict requirements on this, if not, it immediately terminates the process.
- A detailed description of the C ++ optimization code
- Summary Notes on learning and exploring C ++ library functions
- Several minutes to teach you how to use Visual C ++ 6.0 Design programs
- This section describes how to use C ++ and how to modify errors.
- A detailed description of the C ++ optimization code