1

As per my little-bit java knowledge Program supposed to be terminated after it throws runtime exception.

But in my application after throwing runtime exception it is not terminating ,and as i am executing it on linux i have to use ctrl+c to terminate it, Otherwise it just do not terminate.

I am creating jar on windows sytem and copy paste it in linux.

Also i have logging enabled in my application.

Update: I am not catching any exception No multithreading is used.

samarth
  • 3,866
  • 7
  • 45
  • 60
  • are you catching the exception? it will only terminate if it bubbles all the way out of your code. – hvgotcodes Oct 03 '11 at 13:18
  • "after throwing runtime exception" How do you know it is throwing a runtime expcetion? – Raedwald Oct 03 '11 at 13:25
  • @Raedwald Its a custom exception and i can see the exception message – samarth Oct 03 '11 at 13:28
  • "i can see the exception message" and how can you see that? Exception messages do not magically appear. Either the JVM crashed and reported the message (_contra_ your claim), or your code has caught it and logged the message itself, in which case you already know that *you caught the exception*. – Raedwald Oct 03 '11 at 13:33
  • possible duplicate of [Why does the program not terminate when a method that throws RuntimeException is called without handling the exception in Java?](http://stackoverflow.com/questions/27297654/why-does-the-program-not-terminate-when-a-method-that-throws-runtimeexception-is) – Raedwald Dec 06 '14 at 14:10

1 Answers1

18

A RuntimeException (or any Exception or Throwable) does not necessarily terminate your application.

It only terminates your applications if it's thrown on the only non-daemon thread and is not handled.

That means that if either another non-daemon thread is running or you catch the exception, the application will not be terminated.

This recent answer from me gives a summary of what happens (it's specifically about an OutOfMemoryError, but the idea is the same).

Community
  • 1
  • 1
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614