无法在MFC中找到异常处理的设置 [英] Unable to find setting for exception handling in MFC

查看:140
本文介绍了无法在MFC中找到异常处理的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MDI项目(VC ++ 2010专业版)无法捕获错误,但是我回来了,试试catch块。

所以我开发了简单的基于对话框的应用程序。在Dialog上设置了一个按钮

及其代码点击下面代码



  void  CMFCExecDlg :: OnBnClickedButton1()
{
try
{
int j = 0 ;
int i = 10 / j;
}
catch (CException * e)
{
MessageBox(_T( Hello),_ T( ),MB_OK);
}
}





但是程序控制仍然没有进入catch块它只是给出了错误。 />
我尝试了CException的所有子类但没有用。我想在Visual Studio中会有一些设置请帮助我如何处理异常

解决方案

< blockquote>在C ++中,与.NET或Java不同,并非所有情况都有一个单一的父异常类。您无法通过代码捕获所有异常。使用此类通用基本异常类型的 CException 似乎具有误导性;它不是那样的。



为了捕捉所有异常,你需要写下这样的东西:



 尝试 {
// ...
} catch (SomeConcreteExceptionType * e){
// ...
} catch (SomeOtherConcreteExceptionType * e){
// ...
} catch (...){ // 在这里你会抓住所有其余的
// ...
}





许多开发人员说, catch(...)风格不好,但我不同意。糟糕的风格滥用任何东西,包括这种捕捉方法,但是有很多情况下绝对需要这种方法。一个例子:所有线程的最高堆栈框架。



你的想法是什么在你的函数中捕获异常 。您应该尽可能少地捕获异常。你应该简单地放手。在你的功能,任何地方。仅在顶部堆栈帧上捕获异常,并且在相同的极少数情况下,在某些特殊点上捕获异常。例外不是错误。异常旨在将特殊情况的处理与正常执行流程隔离开来。如果你试图在这里和那里捕捉异常,你完全失去了技术的目的。



请参阅我过去的回答:未处理的例外:访问冲突 [ ^ ]。



- SA

My MDI Project(VC++2010 Professional) is unable to catch errors ,though I return ,try catch block.
So I developed simple dialog based application .Placed one button on Dialog
and on its click written following code

void CMFCExecDlg::OnBnClickedButton1()
{
    try
    {
        int j = 0;
        int i = 10/j;
    }
    catch(CException * e)
    {
        MessageBox(_T("Hello"),_T(""),MB_OK);
    }
}



But still program control does not come in catch block it simply gives error.
I tried all child classes of CException but no use.I think there will be some setting in Visual Studio Please Help me for How to handle exceptions

解决方案

In C++, unlike .NET or Java, there is no one single parent exception class for all cases. You cannot catch all exceptions by your code. The seeming similarity of CException with such universal base exception types is misleading; it''s nothing like that.

To catch all exceptions, you would need to write something like this:

try{
    // ...
} catch (SomeConcreteExceptionType * e) {
    // ...
} catch (SomeOtherConcreteExceptionType * e) {
    // ...
} catch (...) { // here you catch all the rest
    // ...
}



Many developers said, catch (...) is bad style, but I disagree. Bad style is abusing anything, including this catch method, but there are number of cases when this is absolutely needed. One example: the very top stack frame of all threads.

What is really bad is your idea to catch exception in your function at all. You should catch exceptions in as little points as possible. You should simply "let go". Out of your function, anywhere. Catch exceptions only on the top stack frame and, in same rare cases, in some special points. Exceptions are not "errors". Exception are designed to isolate handling of special situations from "normal" execution flow. If you try to catch exceptions here and there, you totally defeat the purpose of the technology.

Please see also my past answer: Unhandled Exception : Access Violation[^].

—SA


这篇关于无法在MFC中找到异常处理的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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