Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi consider the following code block
#include<iostream>
int main()
{
    int i=0;
    try{
        int k=9/i;
    }
    catch(...)
    {
     std::cout<<"exception occurs"<<std::endl; //control should come here
    }

}


when I run above code unhandled exception is shown and control doesn't go to catch block
why is it like that?
Posted
Updated 7-Jan-14 1:35am
v2
Comments
uspatel 7-Jan-14 7:25am    
Use int k=9/i;
Rahul@puna 7-Jan-14 7:35am    
thanks for observing that

Beacuse that's not a C++ exception, it is a floating point one, see, for instance, here: "How do I use try…catch to catch floating point errors?"[^].
 
Share this answer
 
Comments
Rahul@puna 7-Jan-14 7:44am    
@CPallini catch(...) is for catching any and all types of exceptions thrown from try block.
or it is only for C++ exception as u mentioned.
CPallini 7-Jan-14 7:54am    
Quoting linked page: "Some of the confusion here may be over the use of the word "exception". In C++, that's usually referring to the language built-in exception handling system. Floating point exceptions are a different beast altogether"
Stefan_Lang 8-Jan-14 4:10am    
+5, very useful link.

This makes me wonder how to control and "catch" other types of system failures such as access violations, stack overflow, etc.. I know how to catch them in the debugger, but I'd much prefer to be able to catch them programmatically in the release version, rather than having the application crash to desktop!
CPallini 8-Jan-14 4:56am    
Thank you.
It does occur - I just tested it.
C++
int main()
    {
    int i=0;
    try{
        int k=9/i;
        }
    catch(...)
        {
        std::cout<<"exception occurs"<<std::endl; //control should come here
        }
    }
It didn't occur in your original version, because the compiler looked at the code, saw a constant divided by a constant and optimised it to a constant value. And then complained about the divide by zero at compile time instead of run time.
 
Share this answer
 
Comments
Rahul@puna 7-Jan-14 7:52am    
@OriginalGriff I have tested the same code as mentioned by you but it stops execution at "int k=9/i" showing unhandled exception while debugging.
Albert Holguin 7-Jan-14 11:22am    
Haven't tried it myself... but did you make sure any optimization is turned off in your compiler?
OriginalGriff 7-Jan-14 11:27am    
Indeed - a good optimise could remove most of that code!
Albert Holguin 7-Jan-14 11:42am    
Yep... and I believe it's on by default in VisualStudio isn't it?
OriginalGriff 7-Jan-14 11:57am    
Not for debug code IIRC.
It is for release code.
Division by zero is not a C++ exception so a try-catch block cannot catch it.

Here are some readings for you:
Catching exception: divide by zero[^]
C++ : Catch a divide by zero error[^]

If you're using a VC++ compiler then you can use structured exception handling for this. Here is an example:

C++
#include <windows.h>
#include <Excpt.h>

int main()
{
   int i = 0;
   __try
   {
      int k = 9 / i;
   }
   __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
   {
      std::cout << "exception occurs" << std::endl; //control should come here
   }

   return 0;
}


You can find more examples in MSDN[^].
 
Share this answer
 

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