Click here to Skip to main content
13,255,820 members (78,821 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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
Posted 24-Jan-13 18:01pm
Updated 24-Jan-13 18:02pm
v2
Comments
Andrew Cherednik 24-Jan-13 23:47pm
   
what kind of error do you see? Is it debug assertion error?
adityarao31 25-Jan-13 7:37am
   
Its not debug assertion. Break and continue error

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100