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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:

//your code
catch(Exception e){e.printStackTrace();System.exit(1);}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?
ecuguruAuthor Commented:
Java OutOfMemory exception indeed.
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).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.