Thread: Exceptions not being handled

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Question Exceptions not being handled

    I decided to read about exceptions finally and the very first example isn't running as expected.

    I'm using VC++. This didn't work on dev-c++ either.

    Here's the code:
    Code:
    #include "stdafx.h"
    
    
    
    
    using std::cout;
    using std::endl;
    
    int main(int argc, char *argv[])
    {
        int top = 90;
        int bottom = 0;
    
        try
        {
            cout <<"top /2 = " << (top / 2) << endl;
            cout <<"top divided by bottom =";
            cout <<(top / bottom)<<endl;
            
            cout <<"top / 3 = " <<(top/3) <<endl;
        }
        catch(...)
        {
                  cout << "An exception has been thrown." <<endl;
        }
        cout <<"Done." <<endl;        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Here's whats in the included header
    Code:
    #pragma once
    
    
    #define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
    #include <stdio.h> //unrelated:why is this needed?
    #include <tchar.h> //unrelated:why is this needed?
    #include <cstdlib>
    #include <iostream>
    Here's what happens.

    It compiles without a complaint but when I run it without debugging instead of the exception being handled by the catch block, windows send error report appears and none of the code following the catch block gets executed. If I run it with debugging it tells me that there is an unhandled exception of (expectedly) integer divide by zero. shouldn't the catch(...) be handling that exception instead of the program crashing?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    catch(...) only handles C++ exceptions. Division by zero is undefined behaviour - on most platforms, it's a hardware exception. C++ catch(...) doesn't catch that. Nor is there any mechanism in standard C++ you can use to catch it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But you might be able to use a signal handler.

    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    void handler ( int sig ) {
        printf( "Caught &#37;d\n", sig );
        exit( 1 );
    }
    
    int main ( ) {
        signal( SIGFPE, handler );
        {
            int a = 1, b = 0;
            int c = a / b;
            printf( "Result=%d\n", c );
        }
        return 0;
    }
    
    $ gcc foo.c
    $ ./a.exe
    Caught 8
    You'll need to read this since windows signals are not quite the same a Unix/Linux signals.

    Also, the amount of work you can do in a signal handler is pretty restrictive. Like for example you know only some functions are safe to call from different threads. It's like that, only the list of functions which are safe to call from a signal handler is smaller still.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Wow. I haven't used a signal handler since the DOS days. I almost completely forgot about signal.

    A more elegant solution, IMO, would be to ensure the denominator cannot be zero since that would cause a divide by zero. In fact there is not a situation where a zero in the denominator would work or be beneficial so I would ensure that you do not allow this to happen.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I just wish there was a way to catch a signal and then convert it to a C++ exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HttpWebResponse Exceptions
    By Boomba in forum C# Programming
    Replies: 1
    Last Post: 01-03-2008, 12:43 PM
  2. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  3. Long-lasting objects that throw exceptions
    By drrngrvy in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2006, 04:30 PM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM