In-depth analysis of how C ++ compiler implements Exception Handling

Source: Internet
Author: User
Tags preg

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:

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

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) ②.

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

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.

 
 
  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. }
  58.  
  59. /* ------- Output -------------------
  60. In the exception handler
  61. 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.

  1. A detailed description of the C ++ optimization code
  2. Summary Notes on learning and exploring C ++ library functions
  3. Several minutes to teach you how to use Visual C ++ 6.0 Design programs
  4. This section describes how to use C ++ and how to modify errors.
  5. A detailed description of the C ++ optimization code

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.