Thread: Mingw32 and C++ exception handling.

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Mingw32 and C++ exception handling.

    This question is not about SEH and is not homework.
    I'm reading the book "Teach yourself c++ in one hour a day".
    Here we have a simple code here from the book
    Code:
    // trying and catching
    #include <iostream>
    using namespace std;
    
    const int DefaultSize = 10;
    
    int main()
    {
        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 << "something has gone wrong!" << endl;
        }
    
        cout << "Done." << endl;
        return 0;
    }
    That code compiled with mingw32 4.5.2 crashes. Compiling it with ms VC++ express 2010 gives the correct answer. What is wrong here? What am I missing with mingw32?
    I read some other threads in this forum, but I didn' t found an answer to this question.

    Thanks.
    Last edited by PauloH; 06-08-2011 at 10:33 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Division by zero results in undefined behaviour. It does not guarantee that an exception will be thrown.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    VC++ has structured exception handling (SEH) that I believe it uses to implement standard c++ handling.

    SEH handles divide by zero, standard exception handling does not.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Thank you.
    You both are right. I just checked VC++ configuration and without SEH it crashes too.

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    /EH (Exception Handling Model) - If you don't specify or use EHa, catch(...) will catch SEH exceptions as well as regular C++ exceptions. If you set it to EHs, it'll only catch C++ exceptions and you'll have to use __try and __except to catch SEH ones.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling
    By thescratchy in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2010, 11:45 AM
  2. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  3. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  4. Exception handling
    By Mr.Sellars in forum C++ Programming
    Replies: 16
    Last Post: 08-10-2007, 10:44 PM
  5. ATL exception handling
    By rzcodeman in forum Windows Programming
    Replies: 1
    Last Post: 06-10-2004, 06:19 PM