捕获空指针异常 [英] catching null pointer exception

查看:113
本文介绍了捕获空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..



我正在用c ++编写代码。我想要做的就是捕获代码中可能发生的所有异常。我正在使用try-catch块。

我的问题是代码没有到达catch块,除非我在try块中给出一个显式抛出。为此,我必须检查我在代码中使用的每个指针。

我想知道的是,有没有办法在我的try块中发生任何异常时自动控制在没有显式抛出的情况下被转移到catch块(就像在java中一样)。



有谁可以帮我这个?

Hi all..

I am writing a code in c++. All I want to do is catch all the exception that can possibly occur in the code. I am using try-catch block for that.
My problem is the code does not reach the catch block until and unless I give an explicit throw in the try block. For doing this I have to check each and every pointer that I am using within the code.
What I want to know is, is there a way that whenever any exception occur in my try block the control automatically gets transferred to the catch block without explicit throw(as is done in java).

Can anyone please help me with this ?

推荐答案

请注意,Microsoft区分C ++异常(由ANSI标准定义)和结构化异常(由MS定义并独立于C ++)。访问冲突产生后者违规。



要捕获结构化异常,您可以使用__try / __finally机制,如解决方案1中所述。



您还可以使用_set_se_translator函数并使用/ EHa进行编译来混合两种类型的异常。这个机制可以帮助你为每个结构化异常生成一个C ++异常。
Note that Microsoft differentiates between C++ exceptions (as defined by the ANSI standard) and Structured Exceptions (as defined by MS and independent of C++). An access violation produces that latter one.

To catch structured exceptions you can use the __try / __finally mechanism as explained in solution 1.

You can also mix both types of exceptions by using the _set_se_translator function and compiling with /EHa. This mechanism helps you produce a C++ exception for every structured exception.


你正确地做了。

在C ++中解引用空指针是未定义的行为,所以没有在c ++中抛出异常(与Java不同)。



如果使用VS> = 2008则可以使用try-except语句



http://msdn.microsoft .com / zh-CN / library / s58ftw19%28v = VS.90%29.aspx [ ^ ]



从经验检查自己的有效指针是最好的方法。
You are doing it correctly.
Dereferencing a null pointer in C++ is undefined behaviour and so no exception is thrown in c++ (unlike Java).

If using VS >= 2008 then it is possible to use try-except Statement

http://msdn.microsoft.com/en-us/library/s58ftw19%28v=VS.90%29.aspx[^]

From experience checking yourself for a valid pointer is the best approach.


它只是意味着你没有真正捕获所有异常。你可以这样做:

It simply means that you did not actually catch all exceptions. This is how you could do it:
try{
    // ...
} catch (...) {
    // ...
}





出于某些原因,这种方法经常受到批评。与所有异常总是从一个异常类型派生的系统(因此它可用于捕获所有可能的异常并仍然具有异常对象)不同,在C ++中情况并非如此:您可以通过绝对任何对象,包括原始型的。当你发现一些未知的异常时,你将不知道如何处理它。



所以,你可以在两种情况下使用这个结构:1)如果你想要阻止所有异常的传播,这是一个非常糟糕的技巧,在一些坏情况下是不可避免的,例如,补偿一些无法修补的不良API中的缺陷; 2)用于纯调试/研究目的。



第二个选项可以帮助你。您可以设置上面显示的catch块的断点,创建一些异常情况,例如整数除零,并查看捕获的类型。然后你可以添加额外的catch块来设计用于捕获一些特定类型的异常对象。



记住异常处理的一些主要原则:将捕获减少到最小,不要' t try-catch本地,放手,处理代码有能力处理它的堆栈中的异常(我称之为能力点),同时,始终处理顶层堆栈上的所有异常每个线程的堆栈的框架。



-SA



This approach is often criticized, for some good reasons. Unlike systems where all exceptions are always derived from one single exception type (so it can be used to catch all possible exception and to still have an exception object), in C++ this is not the case: you can through absolutely any object, including a primitive-type one. When you catch some unknown exception, you won't know what to do with it.

So, you can use this construct in two cases: 1) if you want to block propagation of all exception, which is a pretty bad trick, which can be unavoidable in some "bad" situations, for example, to compensate some defects in bad APIs which are not accessible for patching for your; 2) for pure debugging/research purposes.

The second option can help you well. You can put a break point of the catch block shown above, create some exceptional situation, such as integer division by zero, and see what type is caught. Then you can add additional catch blocks designed to catch some particular types of exception object.

Remember some of the main principles of exception handling: reduce catching to minimum, don't try-catch locally, let go, handle exceptions in the points of the stack where the code is competent to handle it (I call it "points of competence"), at the same time, always handle all exception somewhere on the top stack frame of the stack of each thread.

—SA


这篇关于捕获空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆