Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to catch unknown exception and print it

Tags:

c++

exception

I have some program and everytime I run it, it throws exception and I don't know how to check what exactly it throws, so my question is, is it possible to catch exception and print it (I found rows which throws exception) thanks in advance

like image 549
helloWorld Avatar asked Jun 19 '10 06:06

helloWorld


People also ask

How do you handle an unknown exception?

Retrying your task. The main characteristic of an unknown exception is you can't specifically handle it. Often you won't know why and when it happened and how to work around it. Retrying your task a specific number of times is a useful technique to deal with sporadically unknown exceptions.

How do you handle an unknown exception in C++?

start a debugger and place a breakpoint in the exceptions constructor, and see from where it is being called. the caling function is probably something like __throw(). afterwards, start the debugger again with the program you want to investigate as debuggee.


1 Answers

If it derives from std::exception you can catch by reference:

try {     // code that could cause exception } catch (const std::exception &exc) {     // catch anything thrown within try block that derives from std::exception     std::cerr << exc.what(); } 

But if the exception is some class that has is not derived from std::exception, you will have to know ahead of time it's type (i.e. should you catch std::string or some_library_exception_base).

You can do a catch all:

try { } catch (...) { } 

but then you can't do anything with the exception.

like image 143
R Samuel Klatchko Avatar answered Sep 18 '22 17:09

R Samuel Klatchko