How C ++ compiler can handle exceptions

Source: Internet
Author: User

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:

 
 
  1. 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.

 
 
  1. struct EXCEPTION_REGISTRATION  
  2. {  
  3.     EXCEPTION_REGISTRATION* prev;  
  4.     DWORD handler;  
  5. };  

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:

 
 
  1. 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:

 
 
  1. EXCEPTION_DISPOSITION (*handler)(   
  2. _EXCEPTION_RECORD *ExcRecord,   
  3. void* EstablisherFrame,   
  4. _CONTEXT *ContextRecord,   
  5. 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.

 
 
  1. # Include
  2. # Include
  3.  
  4. Using std: cout;
  5. Using std: endl;
  6.  
  7. Struct prediction_registration
  8. {
  9. Prediction_registration * prev;
  10. DWORD handler;
  11. };
  12.  
  13. Prediction_disposition myHandler (
  14. _ EXCEPTION_RECORD * ExcRecord,
  15. Void * EstablisherFrame,
  16. _ CONTEXT * ContextRecord,
  17. Void * DispatcherContext)
  18. {
  19. Cout<<"In the exception handler"<< Endl;
  20. Cout<<"Just a demo. exiting ..."<< Endl;
  21. Exit (0 );
  22. Return ExceptionContinueExecution; // It will not run to this
  23. }
  24.  
  25. IntG_div=0;
  26.  
  27. Void bar ()
  28. {
  29. // Initialize a prediction_registration Structure
  30. Prediction_registration reg ,*Preg=®
  31. Reg. handler= (DWORD) myHandler;
  32.  
  33. // Obtain the "Header" of the current Exception Handling Link"
  34. DWORD prev;
  35. _ Asm
  36. {
  37. Mov EAX, FS: [0]
  38. Mov prev, EAX
  39. }
  40. Reg. prev= (Prediction_registration *) prev;
  41.  
  42. // Register!
  43. _ Asm
  44. {
  45. Mov EAX, preg
  46. Mov FS: [0], EAX
  47. }
  48.  
  49. // Generate an exception
  50. IntJ=10/G_div; // exception, Division by zero Overflow
  51. }
  52.  
  53. Int main ()
  54. {
  55. Bar ();
  56. Return 0;
  57. }

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.

  1. How to Write C ++ project development and project plan correctly
  2. Summary Notes on learning and exploring C ++ library functions
  3. In-depth demonstration of high security of C ++
  4. Describes in detail how to accurately Write C ++ languages.
  5. In-depth demonstration of high security of C ++

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.