未捕获异常,违反NULL指针访问 [英] Not catching exception, NULL pointer access violation

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

问题描述

大家好,

我有一个如下的代码段

Hello all,

I have a code snippet as follows

struct ItemNode
{
    CString szID;
};

void DoSomething()
{
    ItemNode *a = NULL;

    TRY {
    CString s;
    s.Format (_T("%s"), a->szID);
    }
    CATCH(CException b){
    MessageBox (_T("Exception occured"));
    b->Delete ();
    }
    END_CATCH
    return;
}



在这里,我无法捕捉到异常.该应用程序崩溃,提示存在访问冲突...."
捕获异常的正确方法是什么?
我正在使用MFC,C ++,并且开发环境在VisualStudio 2005中.

http://www的交叉发布.codeproject.com/Forums/1647/C-Cplusplus-MFC.aspx?fid = 1647& select = 3486093& tid = 3486093 [



Here, I am not able to catch the exception. The app crashes saying "There is an access violation.... "
What is the correct way to catch the exception??
I am using MFC, C++ and the development environment is in VisualStudio 2005.

Cross posting of http://www.codeproject.com/Forums/1647/C-Cplusplus-MFC.aspx?fid=1647&select=3486093&tid=3486093[^]

推荐答案

您没有捕获到异常,因为没有C ++异常可以捕获.

发生的事情是您正在取消引用零指针.这会引发操作系统异常,而不是C ++异常. VC ++有多种方法可以将它们转换为另一种方法(在MSDN中查找_se_set_translator),但由于您不这样做,所以没有C ++异常.

其他几点...

-据我所知,如果CString :: Format失败,它不会抛出CException,因此CATCH毫无意义.

-您不必再使用MFC 1.x TRY/CATCH垃圾-可以减少喧闹声,而使用try/catch

-如果您确实使用MFC TRY/CATCH宏,则不必执行b->删除操作.

-您的代码是否可以编译,因为CException和b之间没有逗号?

-如果您不使用TRY/CATCH宏并捕获MFC异常,则必须执行b-> Delete()事情,因为MFC没有遵循按值抛出和按引用捕获的最佳实践

希望有帮助,

干杯,

Ash
You''re not catching an exception because there''s not a C++ exception to catch.

What''s happening is that you''re dereferencing a zero pointer. This raises an operating system exception, NOT a C++ one. There are ways with VC++ that you can convert one to the other (look up _se_set_translator in MSDN) but as you''re not doing them there''s no C++ exception.

A couple of other points...

- As far as I know CString::Format doesn''t throw a CException if it fails so the CATCH is a bit pointless

- You don''t have to use the MFC 1.x TRY/CATCH rubbish anymore - you can be less shouty and use try/catch instead

- If you do use the MFC TRY/CATCH macros you don''t have to do the b->delete thing.

- Did your code compile as there''s not a comma between CException and b?

- If you don''t use the TRY/CATCH macros and catch an MFC exception you have to do the b->Delete() thing as MFC doesn''t follow the best practice of throwing by value and catching by reference

Hope that helps,

Cheers,

Ash


除了Aescleal的回答,我还要说访问冲突是一个严重的问题,不应引起注意.相反,您应该使您的代码正常工作.
如果您仍然想捕获访问冲突,则可以使用 Microsoft特定
^ ]或参见 [尝试/捕获 [
In addition to Aescleal''s answer I would say that access violations are a serious problem and should not be caught. You should instead make your code work properly.
If you still want to catch access violations than you can use the Microsoft specific __try/__except[^] or see this[^] article to catch access violations using standard C++ try/catch[^]. :)


取消引用NULL指针将导致 CPU 抛出通常由处理的硬件异常. >操作系统,通常不是捕获这种异常的好编程风格,相反,最好的方法是在执行可能属于此类异常的代码之前测试条件.
但是,如果您想捕获这种异常,则可以通过三种方式来实现(所有都是 Microsoft特定的功能):

Dereferencing a NULL pointer will cause the CPU to throw an hardware exception that usually is handled by the operating system, and generally is not a good programming style to catch this kind of exception, instead the best is to test conditions before executing code that could fall in such an exception.
However, if you want to catch this kind of exception, you could do it in three ways (all are Microsoft specific features):



    • use the __try/__except construct (see http://msdn.microsoft.com/en-us/library/s58ftw19(VS.80).aspx[^])
  • use SEH (Structured Exception Handling) through AddVectoredExceptionHandler and related API (see http://msdn.microsoft.com/en-us/library/ms679274(VS.85).aspx[^])


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

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