How does the C ++ compiler handle exceptions?

Source: Internet
Author: User
Tags preg

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:

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

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

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. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

Related Article

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.