330

Is there a c++ equivalent of Java's

try {
    ...
}
catch (Throwable t) {
    ...
}

I am trying to debug Java/jni code that calls native windows functions and the virtual machine keeps crashing. The native code appears fine in unit testing and only seems to crash when called through jni. A generic exception catching mechanism would prove extremely useful.

3

16 Answers 16

399
try{
    // ...
} catch (...) {
    // ...
}

will catch all C++ exceptions, but it should be considered bad design. You can use c++11's new current_exception mechanism, but if you don't have the ability to use c++11 (legacy code systems requiring a rewrite), then you have no named exception pointer to use to get a message or name. You may want to add separate catch clauses for the various exceptions you can catch, and only catch everything at the bottom to record an unexpected exception. E.g.:

try{
    // ...
} catch (const std::exception& ex) {
    // ...
} catch (const std::string& ex) {
    // ...
} catch (...) {
    // ...
}
15
  • 78
    It is a good practice to catch exceptions by const reference. As in: catch(std::exception const & ex) { /* ... */ }
    – coryan
    Nov 25, 2008 at 1:18
  • 16
    @coryan: Why is it good practice to catch by const reference?
    – Tim MB
    Nov 9, 2012 at 17:01
  • 27
    Avoiding unnecessary copies is one benefit.
    – Greg D
    Nov 10, 2012 at 21:59
  • 31
    -1: the suggestion that this will "catch all exceptions in C++" is misleading. Try generating a divide by zero error inside the try block. You will see that it will generate an exception that is not caught, yet the code is clearly in C++. It would be more helpful to state that this will "catch all C++ exceptions" and then add some mention of structured exceptions to the notes on limited usefulness.
    – omatai
    Feb 5, 2013 at 1:57
  • 48
    @omatai: Fixed, it will catch all C++ exceptions. Division by zero is undefined behavior and does not generate a C++ exception. Feb 28, 2013 at 23:24
181

Someone should add that one cannot catch "crashes" in C++ code. Those don't throw exceptions, but do anything they like. When you see a program crashing because of say a null-pointer dereference, it's doing undefined behavior. There is no std::null_pointer_exception. Trying to catch exceptions won't help there.

Just for the case someone is reading this thread and thinks he can get the cause of the program crashes. A Debugger like gdb should be used instead.

8
  • 4
    Well, as Shy points out, it is possible with the VC compiler. It's not a good idea, but it is possible.
    – Shog9
    Nov 25, 2008 at 1:25
  • 7
    yeah with SEH. but not with sane standard c++ techniques :) well if you stick to windows you can nearly do everything :) Nov 25, 2008 at 2:08
  • 1
    Mmm... thanks for this tidbit. I've been looking for the answer as to why my null-pointer exceptions aren't beeing caught!
    – user29053
    Aug 20, 2009 at 11:15
  • 11
    You can catch segfaults with SEH on Windows and signal(2)/sigaction(2) on POSIX systems, which covers that vast majority of systems in use today, but like exception handling, it's not something that should be used for normal flow control. It's more of a "do something useful before dying." Oct 11, 2009 at 16:03
  • 1
    @AdamRosenfield until you have implemented try { .. } catch(...) { ... } for catching using signal/sigaction, i wouldn't call it "catching" :) If in a signal handler, it's relatively hard for the programmer to know where in the code the crash happened (i'm talking about programmatically detecting that), compared to try/catch. Apr 22, 2014 at 21:01
109

This is how you can reverse-engineer the exception type from within catch(...) should you need to (may be useful when catching unknown from a third party library) with GCC:

#include <iostream>

#include <exception>
#include <typeinfo>
#include <stdexcept>

int main()
{
    try {
        throw ...; // throw something
    }
    catch(...)
    {
        std::exception_ptr p = std::current_exception();
        std::clog <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
    }
    return 1;
}

and if you can afford using Boost you can make your catch section even simpler (on the outside) and potentially cross-platform

catch (...)
{
    std::clog << boost::current_exception_diagnostic_information() << std::endl;
}
8
  • 2
    what is difference between catch(...) vs catch(std::exception)?
    – Mayur
    Nov 12, 2020 at 11:00
  • 2
    In C++ you can throw anything, an int for instance. ... would catch that too. You “should” catch exceptions by const&, btw.
    – bobah
    Nov 12, 2020 at 15:26
  • FYI, in vs2015, "boost::current_exception_diagnostic_information()" just returns "No diagnostic information available." even with debug information available.
    – Javanator
    Dec 18, 2020 at 16:05
  • 3
    I'm pretty sure that p.__cxa_exception_type()->name() isn't going to be portable. In fact, I just tried it with clang and it wouldn't compile. Aug 20, 2021 at 2:46
  • @EdwardFalk - the first sentence of the answer explicitly says "GCC", so - dah
    – bobah
    Aug 20, 2021 at 8:45
76
try {
   // ...
} catch (...) {
   // ...
}

Note that the ... inside the catch is a real ellipsis, ie. three dots.

However, because C++ exceptions are not necessarily subclasses of a base Exception class, there isn't any way to actually see the exception variable that is thrown when using this construct.

5
  • 25
    In C++11 there is: try { std::string().at(1); // this generates an std::out_of_range } catch(...) { eptr = std::current_exception(); // capture } Jan 1, 2013 at 2:21
  • 2
    @bfontaine: Well yes, but I said that to distinguish the catch specifier from the existing code placeholder in a comment (// ...) which obviously isn't C++ syntax. Jan 17, 2014 at 17:12
  • 1
    @GregHewgill: yes, it was just typographic nitpicking.
    – bfontaine
    Jan 17, 2014 at 21:23
  • 1
    @bfontaine: Fair enough. :) Jan 17, 2014 at 21:52
  • So you mean we cannot log some kind of e.what() on exceptions caught within (...). Right?
    – Maf
    Mar 16 at 12:21
60

it is not possible (in C++) to catch all exceptions in a portable manner. This is because some exceptions are not exceptions in a C++ context. This includes things like division by zero errors and others. It is possible to hack about and thus get the ability to throw exceptions when these errors happen, but it's not easy to do and certainly not easy to get right in a portable manner.

If you want to catch all STL exceptions, you can do

try { ... } catch( const std::exception &e) { ... }

Which will allow you do use e.what(), which will return a const char*, which can tell you more about the exception itself. This is the construct that resembles the Java construct, you asked about, the most.

This will not help you if someone is stupid enough to throw an exception that does not inherit from std::exception.

0
39

In short, use catch(...). However, note that catch(...) is meant to be used in conjunction with throw; basically:

try{
    foo = new Foo;
    bar = new Bar;
}
catch(...)       // will catch all possible errors thrown. 
{ 
    delete foo;
    delete bar;
    throw;       // throw the same error again to be handled somewhere else
}

This is the proper way to use catch(...).

5
  • 7
    its better to using RAII for memory management that automatically handle this exception situations.
    – hich9n
    Mar 29, 2014 at 7:14
  • 1
    @paykoob How does that handle cases where you manged to create a new foo but it failed on a bar. Or when the constructor of bar trys to open a file but fails and therefore throws. then you might end up with a dangeling foo
    – Mellester
    Dec 8, 2014 at 18:39
  • 2
    @MelleSterk Wouldn't the stack still get cleaned up in that case, which would run Foo's destructor? I thought that was the whole point of RAII. However, if you need a pointer to a Foo rather than just creating the Foo on the stack, then you'd need to wrap the pointer in something else that is declared on the stack.
    – reirab
    Apr 4, 2015 at 8:05
  • yes auto foo = std::make_unique<Foo>(); auto bar = std::make_unique<Bar>(); // is exception safe and will not leak, no catch(...) required
    – paulm
    Apr 13, 2015 at 6:54
  • 2
    Me from the future does indeed agree me from the past did not understand RAII at that time
    – Mellester
    Feb 15, 2019 at 21:23
23

it is possible to do this by writing:

try
{
  //.......
}
catch(...) // <<- catch all
{
  //.......
}

But there is a very not noticeable risk here: you can not find the exact type of error that has been thrown in the try block, so use this kind of catch when you are sure that no matter what the type of exception is, the program must persist in the way defined in the catch block.

0
19

You can use

catch(...)

but that is very dangerous. In his book Debugging Windows, John Robbins tells a war story about a really nasty bug that was masked by a catch(...) command. You're much better off catching specific exceptions. Catch whatever you think your try block might reasonably throw, but let the code throw an exception higher up if something really unexpected happens.

2
  • 2
    I just caught some usages of these and peppered in some logging at that stage. Doing nothing with an exception is definitely asking for trouble.
    – jxramos
    May 9, 2016 at 21:24
  • Why trouble? Also can we log some kind of e.what() on exceptions caught within (...)?
    – Maf
    Mar 16 at 12:20
16

Let me just mention this here: the Java

try 
{
...
}
catch (Exception e)
{
...
}

may NOT catch all exceptions! I've actually had this sort of thing happen before, and it's insantiy-provoking; Exception derives from Throwable. So literally, to catch everything, you DON'T want to catch Exceptions; you want to catch Throwable.

I know it sounds nitpicky, but when you've spent several days trying to figure out where the "uncaught exception" came from in code that was surrounded by a try ... catch (Exception e)" block comes from, it sticks with you.

7
  • 2
    Of course, you should never catch Error objects -- if you were supposed to catch them they would be Exceptions. Error objects are completely fatal things, such as running out of heap space etc.
    – SCdF
    Nov 25, 2008 at 2:03
  • 1
    Neither runtime exceptions which are most of the times GoodProgrammerExpected exceptions!!!
    – OscarRyz
    Nov 25, 2008 at 21:21
  • 3
    We had a really serious bug caused by catching an OutOfMemoryError due to a catch(Throwable) block instead of letting it kill things...
    – Hakanai
    Jun 28, 2012 at 2:36
  • 1
    Of course catch(Exception) may not catch all exceptions in Java, you are getting it mixed up with C#... Java = catch(Thowable), C# = catch(Exception). Don't get them confused. Jan 9, 2013 at 14:48
  • 2
    @OscarRyz That sounds like the CoderMalfunctionError (which is actually a real Java Error subclass... though it doesn't mean what it sounds like.)
    – reirab
    Apr 4, 2015 at 8:01
12

Well, if you would like to catch all exception to create a minidump for example...

Somebody did the work on Windows.

See http://www.codeproject.com/Articles/207464/Exception-Handling-in-Visual-Cplusplus In the article, he explains how he found out how to catch all kind of exceptions and he provides code that works.

Here is the list you can catch:

 SEH exception
 terminate
 unexpected
 pure virtual method call
 invalid parameter
 new operator fault 
 SIGABR
 SIGFPE
 SIGILL
 SIGINT
 SIGSEGV
 SIGTERM
 Raised exception
C++ typed exception

And the usage: CCrashHandler ch; ch.SetProcessExceptionHandlers(); // do this for one thread ch.SetThreadExceptionHandlers(); // for each thred


By default, this creates a minidump in the current directory (crashdump.dmp)

10

Be aware

try{
// ...
} catch (...) {
// ...
}

catches only language-level exceptions, other low-level exceptions/errors like Access Violation and Segmentation Fault wont be caught.

6
  • 2
    Things like Segmentation Fault are not actually exceptions, they are signals; thus, you cannot catch them like typical exceptions. However, there are some workarounds like this. Mar 26, 2020 at 15:01
  • what is difference between catch(...) vs catch(std::exception)?
    – Mayur
    Nov 12, 2020 at 11:00
  • 1
    @Mayur catch(...) catches all language level thrown exceptions, on the other hand catch(std::exception) catches only exceptions of type std::exception, for example if you call throw 5 only catch(...) will catch that.
    – muaz
    Nov 12, 2020 at 21:11
  • Is there any additional cost on catching all exceptions?
    – Maf
    Mar 16 at 12:16
  • 1
    @Maf exception handling does not come for free, so it has a cost compared to no exception or error handling, but if you meant: catching all vs catching specific exception, i believe it is cheaper since no exception type check is there, but of course just an opinion with no proof.
    – muaz
    Mar 17 at 14:14
6

A generic exception catching mechanism would prove extremely useful.

Doubtful. You already know your code is broken, because it's crashing. Eating exceptions may mask this, but that'll probably just result in even nastier, more subtle bugs.

What you really want is a debugger...

6
  • 15
    I disagree, there's plenty of cases in real time applications where I'd rather catch an unknown exception, write anything to a log/ pursue some generic error course of action, rather than letting the application crash.
    – f0ster
    Sep 13, 2011 at 16:57
  • 3
    I rather suspect you're thinking of cases where you can pursue some generic error course of action, conveniently ignoring those where the stack is trashed or memory is exhausted and generic error-handling isn't going to succeed either. Nothing wrong with catching errors that you can recover from, but IMHO a catch-all should really exist only as isolated (separate stack, pre-allocated memory), carefully-written logic called just prior to program termination; if you don't know what the problem is, you can't be confident that it can be recovered from.
    – Shog9
    Sep 13, 2011 at 17:22
  • 1
    I.e. install a signal handler which unwinds some log you build during runtime to figure out where the program crashed and, hopefully, why.
    – Clearer
    May 26, 2014 at 14:19
  • For example, I have a suite of unit tests. If one test dies, I want to log it, and then keep testing. Aug 21, 2021 at 5:53
  • @Shog9 I totally disagree. You know that on a crash code is broken, but not where. If the code is in production, you want to log it so you can know what happened . User informations are normally bullshit: they don't know what they have done, everything is random. if you don't know what the problem is - it is almost impossible to find it.
    – Offler
    Nov 17, 2021 at 8:15
4
  1. Can you run your JNI-using Java application from a console window (launch it from a java command line) to see if there is any report of what may have been detected before the JVM was crashed. When running directly as a Java window application, you may be missing messages that would appear if you ran from a console window instead.

  2. Secondly, can you stub your JNI DLL implementation to show that methods in your DLL are being entered from JNI, you are returning properly, etc?

  3. Just in case the problem is with an incorrect use of one of the JNI-interface methods from the C++ code, have you verified that some simple JNI examples compile and work with your setup? I'm thinking in particular of using the JNI-interface methods for converting parameters to native C++ formats and turning function results into Java types. It is useful to stub those to make sure that the data conversions are working and you are not going haywire in the COM-like calls into the JNI interface.

  4. There are other things to check, but it is hard to suggest any without knowing more about what your native Java methods are and what the JNI implementation of them is trying to do. It is not clear that catching an exception from the C++ code level is related to your problem. (You can use the JNI interface to rethrow the exception as a Java one, but it is not clear from what you provide that this is going to help.)

3

For the real problem about being unable to properly debug a program that uses JNI (or the bug does not appear when running it under a debugger):

In this case it often helps to add Java wrappers around your JNI calls (i.e. all native methods are private and your public methods in the class call them) that do some basic sanity checking (check that all "objects" are freed and "objects" are not used after freeing) or synchronization (just synchronize all methods from one DLL to a single object instance). Let the java wrapper methods log the mistake and throw an exception.

This will often help to find the real error (which surprisingly is mostly in the Java code that does not obey the semantics of the called functions causing some nasty double-frees or similar) more easily than trying to debug a massively parallel Java program in a native debugger...

If you know the cause, keep the code in your wrapper methods that avoids it. Better have your wrapper methods throw exceptions than your JNI code crash the VM...

2

If you are looking for Windows-specific solution then there is structured exception handling: https://learn.microsoft.com/en-us/cpp/cpp/try-except-statement

The code looks as follows

__try
{
   // code here may throw or make access violation
}
__except( EXCEPTION_EXECUTE_HANDLER )
{
    // after exception code here, e.g. log the error
}

It will catch not only C++ exceptions but also access violations or other system exceptions.

1

Well this really depends on the compiler environment. gcc does not catch these. Visual Studio and the last Borland that I used did.

So the conclusion about crashes is that it depends on the quality of your development environment.

The C++ specification says that catch(...) must catch any exceptions, but it doesn't in all cases.

At least from what I tried.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.