Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
1.00/5 (1 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

C++
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
Updated 24-Jan-13 17:02pm
v2
Comments
chaau 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

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:

C#
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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900