1

I have a multi-threaded Java application. I want the whole application to fail is one of the thread encounters any exception.

I don't think doing System.exit(); inside the thread will exit the whole app.

Can someone suggest a way?

Bhushan
  • 18,329
  • 31
  • 104
  • 137

3 Answers3

1

actually, calling System.exit() will exit the whole app, but that's generally not what you want in your library code (for instance, it makes unit testing difficult).

a better implementation is to have a shared "error handler" reference, with an implementation that you control. in unit tests, you could just log the exception. in your real app, you could call System.exit().

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
1

put try-catch in Thread's run method and in catch block System.exit(0); it works.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

One simple way to do that is to have try{} catch{} on each thread, when you catch exception you may call static function that exits the application.