• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

join() always needs a try-catch block?

 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

In K&B 6, ch 9, p.726 re: The join() method, we have example code:



Am I right in saying though that the Thread class's join() method always needs a try-catch block round it, because it can throw an InterruptedException? What I mean to say is I've checked in the API and it does, but I'm just wondering if I'm not getting something and there could be circumstances when you don't need a try-catch block.

Thanks

Joe
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Lemmer wrote:Hi there,

In K&B 6, ch 9, p.726 re: The join() method, we have example code:



Am I right in saying though that the Thread class's join() method always needs a try-catch block round it, because it can throw an InterruptedException?

Yes!
Joe Lemmer wrote:
What I mean to say is I've checked in the API and it does, but I'm just wondering if I'm not getting something and there could be circumstances when you don't need a try-catch block.

Thanks

Joe


There are chances to be interrupted the Thread, which in the blocked state. That's why, the join() method should be wrapped around try/catch block!
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Abimaran.

I understand that there is a possibility of the Thread in the blocked state being interrupted. Thats good. I was just wondering why there was no try-catch block round the join() method in K&B. They don't make many mistakes and it was making me a bit nervous!

I am reassured now, though.

Cheers
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because there focus at that time is to give you an idea about join method.
Moreover you can assume that , the method containing that part is ducking the Exception by mentioning throws InterruptedException

Cheers!!!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Lemmer wrote:Thanks Abimaran.

I understand that there is a possibility of the Thread in the blocked state being interrupted. Thats good. I was just wondering why there was no try-catch block round the join() method in K&B. They don't make many mistakes and it was making me a bit nervous!

I am reassured now, though.

Cheers


Check the Errata! They may include it! Or else, check the API!
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Lemmer wrote:I was just wondering why there was no try-catch block round the join() method in K&B.

The code is not complete, so you cannot surely say that there is not try-catch block around it. You can say that its not shown in the code...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic