search
HomeBackend DevelopmentC#.Net TutorialException handling skills for C++ programs

Handling exceptions in C++ comes with a few implicit limitations at the language level, but in some cases you can get around them. By learning various ways to exploit exceptions, you can produce more reliable applications. Preserving exception source information In C++, whenever an exception is caught within a handler, information about the exception source is unknown. The specific source of the 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, you can generate a stack trace in the exception object's constructor during the throw statement. ExceptionTracer is a class that demonstrates this behavior. Listing 1. Generating a stack trace in the exception object constructor // Sample PRogram:

// Compiler: gcc 3.2.3 20030502

// linux: Red Hat #include

#include < ;signal.h> #include

#include 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
{

cout
} free(symbols);

}

}; Governance Signals Whenever a process performs a nasty action that causes the Linux? kernel to emit a signal, the signal must be processed. Signal handlers usually release some important resources and terminate the application. In this case, all object instances on the stack are in an undestructed state. On the other hand, if these signals are converted into C++ exceptions, then you can call their constructors gracefully and arrange multiple layers of catch blocks to better handle these signals. The SignalExceptionClass, defined in Listing 2, provides an abstraction for representing C++ exceptions that may be signaled by the kernel. SignalTranslator is a template class based on SignalExceptionClass, which is usually used to implement conversion to C++ exceptions. At any instant, only one signal handler can handle a signal for 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. Listing 2. Convert signals into exceptions



template class SignalTranslator

{

private:

class SingleTonTranslator

{

public:

SingleTonTranslator()

{

signal( SignalExceptionClass::GetSignalNumber(),

SignalHandler);

} static void SignalHandler(int)

{

throw SignalExceptionClass();

}

}; public:

SignalTranslator()

{

static SingleTonTranslator s_objTranslator;

}

}; // An example for SIGSEGV

class SegmentationFault : public ExceptionTracer, public

exception

{

public:

static int GetSignalNumber() {return SIGSEGV; }

}; SignalTranslator return SIGFPE;}

} ; SignalTranslator

g_objFloatingPointExceptionTranslator; Managing exceptions in constructors and destructors It is impossible for every ANSI C++ to catch exceptions during the construction and destruction of global (static global) variables. Therefore, ANSI C++ does not recommend throwing exceptions in the constructors and destructors of classes whose instances may be defined as global instances (static global instances). In other words, never define global (static global) instances for classes whose constructors and destructors may throw exceptions. However, assuming a specific compiler and a specific system, it might be possible to do so, and fortunately, this is exactly the case with GCC on Linux. This can be demonstrated using the ExceptionHandler class, which also adopts the singleton design pattern. Its constructor registers an uncaught handler. Because only one uncaught handler can handle an active process at a time, the constructor should be called only once, hence the singleton pattern. The global (static global) instance of ExceptionHandler should be defined before the actual global (static global) variable in question is defined. Listing 3. Handling exceptions in constructors class ExceptionHandler


{

private:

class SingleTonHandler

{

public:

SingleTonHandler()

{

set_terminate(Handler);

} static void Handler()

{

// Exception from constrUCtion/destruction of global  variables try

{

// re-throw throw;

}

catch (SegmentationFault &)

{

cout
}

catch (FloatingPointException &)

{

cout
}

catch (...)

{

cout
} //if this is a thread performing some core activity

abort();

// else if this is a thread used to service requests

// pthread_exit();

}

}; public:

ExceptionHandler()

{

static SingleTonHandler s_objHandler;

}

}; ////////////////////////////////////////////////////////////////////////// class A

{

public:

A()

{

//int i = 0, j = 1/i;

*(int *)0 = 0;

}

}; // Before defining any global variable, we define a dummy instance

// of ExceptionHandler object to make sure that

// ExceptionHandler::SingleTonHandler::SingleTonHandler() is 

invoked

ExceptionHandler g_objExceptionHandler;

A g_a; ////////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[])

{

return 0;

}     处理多线程程序中的异常 有时一些异常没有被捕捉,这将造成进程异常中止。不过很多时候,进程包含多个线程,其中少数线程执行核心应用程序逻辑,同时,其余线程为外部请求提供服务。假如服务线程因编程错误而没有处理某个异常,则会造成整个应用程序崩溃。这一点可能是不受人们欢迎的,因为它会通过向应用程序传送不合法的请求而助长拒绝服务攻击。为了避免这一点,未捕捉处理程序可以决定是请求异常中止调用,还是请求线程退出调用。清单3 中 ExceptionHandler::SingleTonHandler::Handler() 函数的末尾处展示了该处理程序。 结束语 我简单地讨论了少许 C++ 编程设计模式,以便更好地执行以下任务: ·在抛出异常的时候追踪异常的来源。

·将信号从内核程序转换成 C++ 异常。

·捕捉构造和/或析构全局变量期间抛出的异常。

·多线程进程中的异常处理。

以上就是C++程序的异常处理技巧的内容,更多相关文章请关注PHP中文网(www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)