Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

threading and exception handling [closed]

Lets say I have 2 threads both executes piece of code one by one, an exception occurs in that piece of code for thread 1, there is no exception handling done in that code, would the second thread get a chance to execute that piece of code.

like image 940
user3499656 Avatar asked Mar 31 '26 18:03

user3499656


1 Answers

An exception thrown in one thread does not affect the execution of code in another thread.

There are some situations where it seems like it does. For example, if something is passed as a Future to another thread, and an exception was thrown when the result of that future was calculated, the thread that calls get() on that future will get an exception. However, this is not one thread affecting the other. The exception is actually stored in the future, and the caller of get() throws it by itself.

But yeah, the answer is, it doesn't affect execution in the second thread..

UPDATE:
Response to the comment: true I don't know why assumed Java. Having said that, the concept of threads having their own stack and the concept of futures seems pretty ubiquitous. For example, Python and C++ have same concepts and they essentially behave the same way. I don't know a platform where threads affected each other like that (probably because it wouldn't make sense).

like image 72
Enno Shioji Avatar answered Apr 03 '26 06:04

Enno Shioji



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!