robert
robert

Reputation: 4867

catch exception qualified by namespace

I have an exception defined like this:

namespace Splat {
    class MyException : public std::exception

and in my code I catch it like this:

} catch (MyException &e) {
    ....
} catch (...) {
    ....

The first catch block where I explicitly declare the exception fails to catch the exception and it is caught in second ... wildcard block.

When I try to qualify the exception declaration with its namespace Splat i.e.

} catch (Splat::MyException &e) {

I get an error:

MyCode.cpp:123: error: expected type-specifier
MyCode.cpp:123: error: expected unqualified-id before ‘&’ token
MyCode.cpp:123: error: expected ‘)’ before ‘&’ token
MyCode.cpp:123 error: expected ‘{’ before ‘&’ token
MyCode.cpp:123: error: ‘e’ was not declared in this scope

It seems to me that this second approach is correct as I haven't explicitly joined Splat into my namespace with using namespace Splat;.

The various examples I have reviewed suggest that including a namespace qualifier in your catch statement is perfectly fine ...

Upvotes: 0

Views: 479

Answers (1)

engf-010
engf-010

Reputation: 3929

The first catch block where I explicitly declare the exception fails to catch the exception and it is caught in second ... wildcard block.

this tells me that MyException is not the same as Splat::MyException !

MyCode.cpp:123: error: expected type-specifier

this tells me that Splat::MyException is not a type and therefore either you use a macro MyException or MyException is a declared identifier (hiding Splat::Exception).

Another posibilty is either you use a macro Splat or Splat is a declared identifier (hiding namespace Splat ,don't know if this is possible).

Upvotes: 2

Related Questions