Can we propagate exceptions in threads?

Prior to Java1.5 we cannot because the run() method in Runnable interface is written such that there is no return value as well as return exceptions in the method signature and hence the overridden method cannot propagate exceptions.

In Java1.5 SUN has released java.util.concurrent package in which alternative to Runnable i.e, Callable is introduced which can return any type and throw any subtype of Exception.

So,
– Using Runnable interface, one cannot propagate the exception and cannot return any value (return type is ‘void’), hence we should go for try-catch block to handle the exception
– Using Callable interface, we can propagate exception( or return a value) to the parent thread where we invoked the callable thread.

Sample code demonstrating the usage is uploaded in GITHub
https://github.com/ntallapa/TechForumWorkspace

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Mawazo

Mostly technology with occasional sprinkling of other random thoughts

amintabar

Amir Amintabar's personal page

101 Books

Reading my way through Time Magazine's 100 Greatest Novels since 1923 (plus Ulysses)

Seek, Plunnge and more...

My words, my world...

ARRM Foundation

Do not wait for leaders; do it alone, person to person - Mother Teresa

Executive Management

An unexamined life is not worth living – Socrates

javaproffesionals

A topnotch WordPress.com site

thehandwritinganalyst

Just another WordPress.com site

coding algorithms

"An approximate answer to the right problem is worth a good deal more than an exact answer to an approximate problem." -- John Tukey

%d bloggers like this: