Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ get description of an exception caught in catch(...) block

can I get description of an exception caught by

catch(...) 

block? something like .what() of std::exception.

like image 632
MBZ Avatar asked Sep 04 '10 09:09

MBZ


People also ask

How do you catch exceptions in catch block?

Place any code statements that might raise or throw an exception in a try block, and place statements used to handle the exception or exceptions in one or more catch blocks below the try block. Each catch block includes the exception type and can contain additional statements needed to handle that exception type.

How do I get an exception message in C++?

C++ exception handling is built upon three keywords: try, catch, and throw. throw − A program throws an exception when a problem shows up. This is done using a throw keyword. catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem.

What if exception happens in catch block?

Answer: When an exception is thrown in the catch block, then the program will stop the execution. In case the program has to continue, then there has to be a separate try-catch block to handle the exception raised in the catch block.

Can we use catch ()' without passing arguments in?

Some exception can not be catch(Exception) catched. Below excecption in mono on linux, should catch without parameter. Otherwise runtime will ignore catch(Exception) statment.


2 Answers

There is one trick you might be able to use:

catch(...) {     handle_exception(); }  void handle_exception() {     try {         throw;     } catch (const std::exception &e) {         std::cout << e.what() << "\n";     } catch (const int i) {         std::cout << i << "\n";     } catch (const long l) {         std::cout << l << "\n";     } catch (const char *p) {         std::cout << p << "\n";     } catch (...) {         std::cout << "nope, sorry, I really have no clue what that is\n";     } } 

and so on, for as many different types as you think might be thrown. If you really know nothing about what might be thrown then even that second-to-last one is wrong, because somebody might throw a char* that doesn't point to a nul-terminated string.

It's generally a bad idea to throw anything that isn't a std::exception or derived class. The reason std::exception exists is to allow everybody to throw and catch objects that they can do something useful with. In a toy program where you just want to get out of there and can't even be bothered to include a standard header, OK, maybe throw an int or a string literal. I don't think I'd make that part of a formal interface. Any exceptions you throw are part of your formal interface, even if you somehow forgot to document them.

like image 132
Steve Jessop Avatar answered Sep 24 '22 03:09

Steve Jessop


That block might catch an int, or a const char*, or anything. How can the compiler possibly know how to describe something when it knows nothing about it? If you want to get information off an exception, you must know the type.

like image 23
Puppy Avatar answered Sep 22 '22 03:09

Puppy