tags:

views:

4162

answers:

10

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  A: 
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.

Greg Hewgill
+3  A: 

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.

John D. Cook
+18  A: 
try{
    // ...
} catch (...) {
    // ...
}

will catch all exceptions in C++, but it's of limited usefulness. Note, for example, the absence of a name for the exception you're catching. You're not guaranteed to get any sort of message at all in that context. 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 (...) {
    // ...
}
Greg D
coryan
@coryan, Thanks for the reminder. I've been spending too much time in C# land lately. :)
Greg D
+5  A: 

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.

McWafflestix
Wasn't useful when it got me! :-) Hehe, thanks though...
McWafflestix
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
Neither runtime exceptions which are most of the times GoodProgrammerExpected exceptions!!!
OscarRyz
+2  A: 

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...

Shog9
+5  A: 

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.

Johannes Schaub - litb
Well, as Shy points out, it is possible with the VC compiler. It's not a good idea, but it is possible.
Shog9
yeah with SEH. but not with sane standard c++ techniques :) well if you stick to windows you can nearly do everything :)
Johannes Schaub - litb
Mmm... thanks for this tidbit. I've been looking for the answer as to why my null-pointer exceptions aren't beeing caught!
Dalin Seivewright
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."
Adam Rosenfield
+1  A: 
  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.)

orcmid
+2  A: 

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.

Clearer
A: 

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...

mihi
A: 

I am using macros to put try/catch blocks in the code. These macros logs the exceptions when they occur. If it is not a problem I want to give the link of codeproject page: http://www.codeproject.com/KB/cpp/exceptionlogging.aspx

CPP coder