I've got a native C++ code that starts a JVM and calls Java methods. One of those Java methods calls native codes which may contain mistakes that need to be debugged. However, whenever a fatal error (such as access violation) is raised from the native code (C and C++) called through JNI, JVM automatically aborts the program, while it should let the debugger show me the problematic part of the code and other useful information. I'm currently limited to developing on Windows with Visual Studio 2013 Express but suggestions for other development environments are also welcome.
Asked
Active
Viewed 942 times
1
-
Is SEH (Structured Exception Handling) turned on when you built your JNI module? If so, then access violations can be caught using traditional `try/catch`. – PaulMcKenzie Sep 06 '14 at 06:35
-
Oh, with Visual Studio, it is `Properties -> Configuration Properties -> C/C++ -> Code Generation`. Make sure that "Enable C++ Exceptions" is set to "Yes, with SEH". – PaulMcKenzie Sep 06 '14 at 06:38
-
@PaulMcKenzie I tried SEH exception with try/catch block and this approach does a good job of pausing the execution of the program (with breakpoints) and preventing the JVM from automaticaly aborting the program. However, I wasn't able to pinpoint the location of the error (file and line number) which I'd like to have especially when there are many nested function calls. – kes5219 Sep 06 '14 at 06:56
-
Well, maybe this can help any: http://stackoverflow.com/questions/2782915/what-should-i-know-about-structured-exceptions-seh-in-c But did my advice at least stop the JVM from crashing? If so, I would like to post that as an answer. – PaulMcKenzie Sep 06 '14 at 07:21
-
@PaulMcKenzie Yes, this method was also able to stop JVM from crashing, and it looks like I can at least gather some debugging information from the thrown exception by calling GetExceptionInformation function. I think your answer is the most effective solution I've come across so far, so please feel free to answer this question with what you've written in the comments. – kes5219 Sep 06 '14 at 07:45
1 Answers
1
Make sure that you have built your JNI module with the Structured Exception Handling
turned on.
In the Visual Studio IDE:
Properties -> Configuration Properties -> C/C++ -> Code Generation -> Enable C++ Exceptions
Make sure that "Yes, with SEH" is selected. This will now allow you to use traditional try/catch
to handle exceptions such as access violations.

PaulMcKenzie
- 34,698
- 4
- 24
- 45