Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To throw a Thread

Tags:

java

I have a question.

Would it be possible, hypthetically, in Java, to throw a running thread as exception? I know this is madness, but just for fun, could it be done? Something along the lines of:

public void throwThread() throws ExceptableThread {
    ExceptableThread thread = new ExceptableThread();
    thread.start();
    throw thread;

Implementing Runnable as interface is not really an option. It should throw the actual thread object.

like image 436
Stellarator Avatar asked Jan 28 '26 06:01

Stellarator


2 Answers

No, it wouldn't. Thread doesn't extend Throwable, so you can't throw it.

Even if it were possible, it would be a horrible conflation of two very separate responsibilities, IMO.

like image 107
Jon Skeet Avatar answered Jan 30 '26 18:01

Jon Skeet


You can throw an exception with contains a Thread - weird things _

like image 24
Matthias Bruns Avatar answered Jan 30 '26 19:01

Matthias Bruns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!