0

Exceptions are not being caught in a case where I expect them to be caught. The code is in 1 function in 1 cpp file which is compiled into a static library by GCC 4.2 and then linked into a Cocoa application. The code in question is

class runtime_error : public exception{
// More code
};


int foo( void ){
    try {
        if( x == 0 ){
            throw std::runtime_error( "x is 0" );
        }
    }
    catch( std::exception & e ){
    // I expect the exception to be caught here
    }
    catch( ... ){
        // But the exception is caught here
    }
}   

I can modify the code to be

int foo( void ){
    try {
        if( x == 0 ){
            throw std::runtime_error( "x is 0" );
        }
    }
    catch( std::runtime_error & e ){
    // exception is now caught here
    }
    catch( … ){
    }
}

The second version of the code only solves the problem for runtime_error exceptions and not other exception classes that might be derived from std::exception. Any idea what is wrong? Note the first version of the code works fine with Visual Studio.

Thanks,

Barrie

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
Barrie
  • 11
  • 4
  • Is there any reason you're defining your own `runtime_error` class when one already exists? – templatetypedef Feb 13 '12 at 17:53
  • 1
    The exception should be caught. http://ideone.com/dl0Dm Update your GCC and see if the bug still happens – Kos Feb 13 '12 at 17:55
  • The code you've shown us doesn't compile. Please provide a short, self-contained complete example. http://sscce.org – Robᵩ Feb 13 '12 at 17:58
  • There is some confusion about the definition of std::runtime_error. I don't actually define it. I am simply showing that it is publicly derived from std::exception and hence the exception should be caught. – Barrie Feb 13 '12 at 23:57
  • I've discovered that a 3rd party library is causing this problem. If I link the library (but do not call any of it's code) then the problem occurs. If I do not link the 3rd party library then the problem does not occur. Any idea's how linking a 3rd party static library could cause this problem? Thanks – Barrie Feb 21 '12 at 18:13
  • That is incorrect. You are indeed defining a class and not providing a forward declaration as you expect. – ppl May 24 '13 at 19:16

2 Answers2

1

Your code doesn't compile as written. When I change it as below to add needed includes, variables, etc, it prints "exception" as expected (g++ 4.2 and 4.5). Can you show us the full real code that's causing your problem?

#include <exception>
#include <stdexcept>
#include <iostream>

int x = 0;

int foo( void ){
    try {
        if( x == 0 ){
            throw std::runtime_error( "x is 0" );
        }
    }
    catch( std::exception & e ){
        // I expect the exception to be caught here
        std::cout << "exception" << std::endl;
    }
    catch( ... ){
        // But the exception is caught here
        std::cout << "..." << std::endl;
    }

    return 0;
}

int main()
{
    foo();

    return 0;
}
Mark B
  • 95,107
  • 10
  • 109
  • 188
  • Here is some code which I have just tested which run's and fails in the way originally described: #include "stdafx.h" int foo(void) { try { throw std::runtime_error("Error"); return 0; } catch (std::exception &e) { return 1; } catch (...) { return 1; } } – Barrie Feb 13 '12 at 23:58
0

Your class runtime_error is a class defined in your code's namespace. I'm not sure why would you use it with std:: as a scope resolution operator?

Shouldn't the line throw std::runtime_error( "x is 0" ); be changed to throw runtime_error( "x is 0" );?

vvnraman
  • 1,322
  • 13
  • 21
  • There is some confusion about the definition of std::runtime_error. I don't actually define it. I am simply showing that it is publicly derived from std::exception and hence the exception should be caught. – Barrie Feb 13 '12 at 23:57