Easily solve C ++ exceptions

Source: Internet
Author: User

When handling C ++ exceptions, you may encounter some Implicit restrictions at the language level, but in some cases, you can bypass them. By learning a variety of exception methods, you can produce more reliable applications. This article will analyze and introduce the C ++ exception problems that most users have a headache.

In C ++, whenever an exception is captured in the handler, information about the exception source is unknown. The specific source of an exception can provide a lot of important information to better handle the exception, or provide some information that can be appended to the error log for later analysis.

To solve this problem, a stack trace can be generated in the constructor of the exception object during the exception statement throw. Predictiontracer is a class that demonstrates such behavior.

Generate a stack trace in the exception object constructor:

 
 
  1. private:  
  2. class SingleTonTranslator  
  3. {  
  4. public:  
  5. SingleTonTranslator()  
  6. {  
  7. signal(SignalExceptionClass::GetSignalNumber(),   
  8. SignalHandler);  
  9. }  
  10.  
  11. static void SignalHandler(int)  
  12. {  
  13. throw SignalExceptionClass();  
  14. }  
  15. };  
  16.  
  17. public:  
  18. SignalTranslator()  
  19. {  
  20. static SingleTonTranslator s_objTranslator;  
  21. }  
  22. };  
  23.  
  24. // An example for SIGSEGV  
  25. class SegmentationFault : public ExceptionTracer, public   
  26. exception  
  27. {  
  28. public:  
  29. static int GetSignalNumber() {return SIGSEGV;}  
  30. };  
  31.  
  32. SignalTranslator<SegmentationFault>   
  33. g_objSegmentationFaultTranslator;  
  34.  
  35. // An example for SIGFPE  
  36. class FloatingPointException : public ExceptionTracer, public   
  37. exception  
  38. {  
  39. public:  
  40. static int GetSignalNumber() {return SIGFPE;}  
  41. }; 

Every time a process executes an annoying action, so that Linux? When the kernel sends a signal, the signal must be processed. Signal processing programs usually release some important resources and terminate applications.

In this case, all the object instances on the stack are in the undamaged state. On the other hand, if these signals are converted to C ++ exceptions, You can elegantly call their constructor and arrange multi-layer catch blocks to better process these signals.

The defined SignalExceptionClass provides an abstraction of the C ++ exception that indicates the kernel may send signals. SignalTranslator is a template class based on SignalExceptionClass, which is usually used to implement conversion to C ++ exceptions.

At any moment, only one signal processing program can process one signal of an active process. Therefore, SignalTranslator adopts the singleton design mode. The overall concept is presented through the SegmentationFault class for sigsegv and the FloatingPointException class for SIGFPE.

During the construction and Analysis of Global static global variables, it is impossible for every ansi c ++ to catch exceptions. Therefore, ansi c ++ does not recommend that you throw an exception in the constructor and destructor of classes whose instances may be defined as Global static global instances.

  • Why should I learn C ++ programs?
  • Analysis of C ++ function call Methods
  • How to call C ++
  • How to Implement Visual C ++ System Tray
  • Study on the origins of C ++ Language

In other words, we should never define Global static global instances for classes whose constructors and destructor may throw exceptions. However, if we assume there is a specific compiler and a specific system, we may be able to do this. Fortunately, this happens to GCC on Linux.

You can use the ExceptionHandler class to demonstrate this. This class also adopts the singleton design mode. Its constructor registers an uncaptured handler. Because only one uncaptured handler can process one active process at a time.

The constructor should be called only once. Therefore, use the singleton mode. You should define the global static global of predictionhandler before defining the problematic Global static global) instance.

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.