5

I am a Java programmer working with C++ code, and need some help with exception handling.

I have code in the following structure:

try{
...
}
catch( ... ) 
{
    log("Exception occurred");
}

An exception is occurring, but the try block is really massive and debugging is not an option, so I need to minimally modify the code to give me relevant info on the exception.

So I added the following catch block before the existing catch block (working with my Java knowledge and referring to C++ guides):

catch(exception e)
{
    log(e.what());
}

However, I am still getting the old message - "Exception occurred". The code in try block contains a lot of low level functions like strncpy, memcpy etc.

Why is this catch block not working as expected? What can I do to get information on the exception that is occurring and on which line, similar to the stack trace that Java so easily gives.

Mankarse
  • 39,818
  • 11
  • 97
  • 141
Shailesh Tainwala
  • 6,299
  • 12
  • 58
  • 69
  • There is no such thing as an "exception" in `C` (at least not in the sense of a control-flow mechanism). – Mankarse May 10 '12 at 07:26
  • 1
    @Mankarse, `std::exception` does exist in C++ standard library. To Shailesh, you can use gdb to debug such non-standard exceptions. – iammilind May 10 '12 at 07:29
  • possible duplicate of [Getting the backtrace from the catch block](http://stackoverflow.com/questions/4283943/getting-the-backtrace-from-the-catch-block) – BЈовић May 10 '12 at 07:33
  • @iammilind: This question was previously tagged C, and talked about handling exception handling in "C/C++" code. It has since been edited, as Shailesh Tainwala is clearly writing C++, not C. – Mankarse May 10 '12 at 07:34
  • 1
    worth to mention that you should put your catch block before the catch(...), also you should catch by reference and not by value – Alessandro Teruzzi May 10 '12 at 07:36
  • @AlessandroTeruzzi: In the question the guy specified that he put his catch block _before_ `catch( ... )`. – Jagger May 10 '12 at 07:45
  • sorry, you are correct, I missed it. – Alessandro Teruzzi May 10 '12 at 07:48

3 Answers3

6

First, you should catch by reference (generally const), so your new catch block should read:

try {

} catch(std::exception const& e) {
    log(e.what());
} catch(...) {
    log("Exception occurred");
}

Second, in C++ you may throw any value. Even of type int. If your codebase include such unsavvy throw statements, I pity you.

Since you come from Java, I would check if you mistakenly used a throw new XXXX which would throw a pointer (to a dynamically allocated value) instead of a value. The new is unnecessary in C++.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
3

Probably because those exceptions are not derived from exception class. In C++ any type can be an exception, for example string, int, etc. Anyway if you want to catch exception you should probably catch a reference to the exception &.

Jagger
  • 10,350
  • 9
  • 51
  • 93
  • If the existing catch-all handler didn't catch the exception, the exception thrown is not an c++ exception at all. – Alok Save May 10 '12 at 07:34
2

You will have to debug and determine if the exception is an C++ exception.
Note that divide by 0 etc are runtime exceptions not supported by C++, so it can be anything literally.

A catch-all handler will only catch valid C++ exceptions not all exceptions(which can be plenty).

Alok Save
  • 202,538
  • 53
  • 430
  • 533