How to throw a checked exception from a java thread?

后端 未结 9 2168
长发绾君心
长发绾君心 2020-11-28 04:45

Hey, I\'m writing a network application, in which I read packets of some custom binary format. And I\'m starting a background thread to wait for incoming data. The problem i

9条回答
  •  无人及你
    2020-11-28 05:25

    To be able to send the exception to the parent thread, you can put your background thread in a Callable (it allows throwing also checked exceptions) which you then pass to the submit method of some Executor. The submit method will return a Future which you can then use to get the exception (its get method will throw an ExecutionException which contains the original exception).

提交回复
热议问题