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
  • 17,383
  • 27
  • 100
  • 136

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
  • 51,973
  • 5
  • 73
  • 115
1

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

Subhrajyoti Majumder
  • 39,719
  • 12
  • 74
  • 101
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.