In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to solve the abnormal problems of C++. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.
There are some implicit limitations at the language level when dealing with C++ exceptions, but in some cases you can bypass them. By learning various ways to take advantage of exceptions, you can produce more reliable applications. This article provides a detailed analysis and introduction to the C++ exception, which is a headache for most users.
In C++, whenever an exception is caught within a handler, the information about the source of the exception is unknown. The specific source of the exception can provide a lot of important information to better handle the exception, or some information that can be attached to the error log for later analysis.
To solve this problem, you can generate a stack trace in the constructor of the exception object during the exception statement. ExceptionTracer is a class that demonstrates this behavior.
Generate a stack trace in the exception object constructor:
Private: class SingleTonTranslator {public: SingleTonTranslator () {signal (SignalExceptionClass::GetSignalNumber (), SignalHandler);} static void SignalHandler (int) {throw SignalExceptionClass ();}}; public: SignalTranslator () {static SingleTonTranslator sliced objTranslator;}}; / / An example for SIGSEGV class SegmentationFault: public ExceptionTracer, public exception {public: static int GetSignalNumber () {return SIGSEGV;}}; SignalTranslator g_objSegmentationFaultTranslator / / An example for SIGFPE class FloatingPointException: public ExceptionTracer, public exception {public: static int GetSignalNumber () {return SIGFPE;}}
Every time a process performs an annoying action, so that Linux? When the kernel sends a signal, the signal must be processed. Signal handlers usually release important resources and terminate the application.
In this case, all object instances on the stack are in an unbroken state. On the other hand, if these signals are converted into C++ exceptions, you can gracefully call their constructors and arrange multiple layers of catch blocks to better handle the signals.
The defined SignalExceptionClass provides an abstraction for C++ exception problems that may be signaled by the kernel. SignalTranslator is a SignalExceptionClass-based template class that is commonly used to convert to C++ exceptions.
At any moment, only one signal processor can process one signal from an active process. Therefore, SignalTranslator adopts the singleton design pattern. The overall concept is demonstrated through the SegmentationFault class for SIGSEGV and the FloatingPointException class for SIGFPE.
It is impossible for every ANSI C++ to catch an exception during the construction and destruction of a global (static global) variable. Therefore, ANSI C++ does not recommend throwing exceptions in the constructors and destructors of classes whose instances might be defined as global instances (static global instances).
To put it another way, never define global (static global) instances for classes whose constructors and destructors may throw exceptions. However, if you assume that you have a specific compiler and a specific system, you may be able to do so, and fortunately for GCC on Linux, this is exactly the case.
This can be demonstrated using the ExceptionHandler class, which also adopts the singleton design pattern. Its constructor registers an uncaptured handler. Because only one uncaptured handler can handle one active process at a time.
The constructor should only be called once, so use the singleton mode. You should define a global (static global) instance of ExceptionHandler before defining the actual global (static global) variable in question.
After reading the above, do you have any further understanding of how to solve the abnormal problem of C++? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.