tags:

views:

98

answers:

4

hello to everyone, 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

+3  A: 

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.

R Samuel Klatchko
@R Samuel Klatchko: thanks a lot, one more question, can I using your method check exceptions of new and delete?
helloWorld
@helloWorld - yes, this will catch exceptions thrown from `new`, `delete` and any constructor or destructor called by them (provided the `new` or `delete` statement is within the `try` block).
R Samuel Klatchko
A: 

Can you elaborate what exact exception is thrown?

I suggest you identify why the exception is thrown and work towards preventing the exception from occuring.

ckv
A: 

Try as suggested by R Samuel Klatchko first. If that doesn't help, there's something else that might help:

a) Place a breakpoint on the exception type (handled or unhandled) if your debugger supports it.

b) On some systems, the compiler generates a call to an (undocumented?) function when a throw statement is executed. to find out, what function that is for your system, write a simple hello world program, that throws and catches an exception. 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. place breakpoint on the function mentioned above (_throw or whatever) and run the program. when the exception is thrown, the debugger stops and you are right there to find out why.

Axel
A: 

If the same row throws an exception, it means there is a problem with your code. Remember exceptions should be exceptional and not an expected one.. And the program flow based on the exceptions is a poor practice.

The exceptions you were asking about new and delete might be due to the deletion of released memory, accessing a pointer that has been allocated no memory, etc., Take another look at your memory allocation and de-allocation..

Hope it helps..

liaK