Java: Die on error / detect frozen?

I'm still debugging a java application I wrote, that does a lot of work with databases.  Occasionally, I get a java heap error or local memory error.  Right now, it prints out the error message and the app just stops.  It doesn't die, it just sits there with the printed out error message.

I'm running the application from a batch file:
java -jar myapp.jar

Is there a way to setup the java environment, or the app, so that on error like heap/memory, kill the app?
Best thing i to find the bug and squash it, but for resiliency, I'd like to be able to keep the app running after crash.  If it exists, I can detect that and restart it (I think) by setting it up as a service in Windows.
ecuguruAsked:
Who is Participating?
 
MicheleMarconConnect With a Mentor Commented:
It is probably a JRE bug, so you should post it to the sun website.

Are you using JNI?

If instead is a bug in your code, surrond the code with this block:

try{
//your code
}
catch(Exception e){e.printStackTrace();System.exit(1);}
0
 
pellepCommented:
Could you please elaborate on what type of error it is you're getting. Is it a windows error (windows msgbox popup)? Is it a java OutOfMemory exception that's thrown?
0
 
ecuguruAuthor Commented:
Java OutOfMemory exception indeed.
0
 
pellepConnect With a Mentor Commented:
You can try what MicheleMarcon suggested, except you should catch Throwable, not Exception. OutOfMemory is a subclass of Error, not Exception so you'll need to catch the "super" interface (Throwable).

Just wrap the code in your main() function in a try/catch, catch Throwable and do a System.exit(1) like MichelleMarcon suggest (which should forcibly terminate your application).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.