Exceptions in the C ++ program may encounter some Implicit restrictions at the language level, but in some cases, you can bypass them. By learning a variety of ways to use exceptions, you can produce more reliable applications. I hope this article will bring you useful things.
Retain exception source information
In a C ++ program, whenever an exception is caught in the processing program, 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.
Listing 1. Generate a stack trace in the exception object constructor
- // Sample Program:
- // Compiler: gcc 3.2.3 20030502
- // Linux: Red Hat
- #include <execinfo.h>
- #include <signal.h>
- #include <exception>
- #include <iostream>
- using namespace std;
- /////////////////////////////////////////////
- class ExceptionTracer
- {
- public:
- ExceptionTracer()
- {
- void * array[25];
- int nSize = backtrace(array, 25);
- char ** symbols = backtrace_symbols(array, nSize);
-
- for (int i = 0; i < nSize; i++)
- {
- cout << symbols[i] << endl;
- }
- free(symbols);
- }
- };
Manage Signals
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 ++ program exceptions, You can elegantly call their constructor and arrange multi-layer catch blocks to better process these signals.
The SignalExceptionClass defined in Listing 2 provides an abstraction of the C ++ exception that represents a possible signal from the kernel.
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.
Listing 2. Converting signals to exceptions
- // Sample Program:
- // Compiler: gcc 3.2.3 20030502
- // Linux: Red Hat
- #include <execinfo.h>
- #include <signal.h>
- #include <exception>
- #include <iostream>
- using namespace std;
- /////////////////////////////////////////////
- class ExceptionTracer
- {
- public:
- ExceptionTracer()
- {
- void * array[25];
- int nSize = backtrace(array, 25);
- char ** symbols = backtrace_symbols(array, nSize);
-
- for (int i = 0; i < nSize; i++)
- {
- cout << symbols[i] << endl;
- }
- free(symbols);
- }
- };
Handle C ++ program exceptions in multi-threaded programs
Sometimes some exceptions are not caught, which will cause the process to stop. However, in many cases, a process contains multiple threads, a few of which execute the core application logic. At the same time, other threads provide services for external requests. If the service thread does not handle an exception due to a programming error, the entire application will crash. This may be undesirable because it promotes denial-of-service attacks by sending illegal requests to applications. To avoid this, the uncaptured handler can decide whether the call is aborted due to an exception in the request or whether the request thread exits the call.
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design