mercredi 27 juin 2018

Is there any way to print its information when met an uncaught exception that is not from STL?

When compiler meets an exception that is from STL like std::out_of_range :

int main(int argc, char *argv[]) {
    throw std::out_of_range("There is an exception!");
}

the console will show the message :

libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: There is an exception!

So I wrote an exception class :

class Exception {
    protected:
        const char *msg;
    public:
        Exception(const char *msg) noexcept : msg(msg) {}
        Exception(const string &msg) noexcept : msg(msg.c_str()) {}
        const char *what() const noexcept {
            return this->msg;
        }
};

However, throwing Exception will not get any information :

int main(int argc, char *argv[]) {
    throw Exception("There is an exception!");
}

Console message :

libc++abi.dylib: terminating with uncaught exception of type Exception

Is there any way making console show :

libc++abi.dylib: terminating with uncaught exception of type Exception: There is an exception!

Compiler : Apple LLVM version 9.1.0 (clang-902.0.39.2)

Aucun commentaire:

Enregistrer un commentaire