简体   繁体   中英

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 ie

} 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 ...

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM