Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFCVC++
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 17:01pm
Edited 24-Jan-13 17:02pm
v2
Comments
Andrew Cherednik at 24-Jan-13 23:47pm
   
what kind of error do you see? Is it debug assertion error?
adityarao31 at 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
0 Dnyaneshwar@Pune 1,300
1 Sergey Alexandrovich Kryukov 660
2 OriginalGriff 309
3 ravikhoda 300
4 Manas Bhardwaj 240
0 Sergey Alexandrovich Kryukov 11,194
1 OriginalGriff 6,867
2 Peter Leow 4,795
3 Abhinav S 3,838
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid