Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception java.lang.InterruptedException is never thrown in body of corresponding try statement

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea why I am getting a compilation error, and, how I could go about it?



Source Core Java Vol.2 (7th Ed.) written for JDK 1.5.

Error java:52: exception java.lang.InterruptedException is never thrown in body of corresponding try statement catch (InterruptedException e)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the error message from the compiler carefully.

What do you think it is trying to tell you?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Were you able to figure out the source of error in the below code?
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:Were you able to figure out the source of error in the below code?


Well is it safe to remove the catch? In any case the 'ball' does not seem to move anywhere with this code, when I remove the catch clause.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:Well is it safe to remove the catch? In any case the 'ball' does not seem to move anywhere with this code, when I remove the catch clause.

So, let's look at the error message from the compiler:

exception java.lang.InterruptedException is never thrown in body of corresponding try statement catch (InterruptedException e)

Read it carefully and try to understand it. What does it say? It says that in the try-block there isn't any code that can ever throw InterruptedException. In other words, catching InterruptedException is unnecessary here, because it will never be thrown from the try-block.

So, what do you think, is it safe to remove the catch?
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Jon Camilleri wrote:Well is it safe to remove the catch? In any case the 'ball' does not seem to move anywhere with this code, when I remove the catch clause.

So, let's look at the error message from the compiler:

exception java.lang.InterruptedException is never thrown in body of corresponding try statement catch (InterruptedException e)

Read it carefully and try to understand it. What does it say? It says that in the try-block there isn't any code that can ever throw InterruptedException. In other words, catching InterruptedException is unnecessary here, because it will never be thrown from the try-block.

So, what do you think, is it safe to remove the catch?


Horstmann reads that the catch is recommended to be there, whilst the compiler thinks that it is not.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the code in the try-block will never throw InterruptedException, catching this exception is unnecessary, you should remove the catch for that exception. If the code is exactly copied from the book (and not modified in any way) then that is a mistake in the book.

The compiler is always right. (Unless there is a bug in the compiler, but it will be very, very rare to encounter a bug in the compiler).
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just double check that you have not mistakenly commented the below statement which is the cause for the error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic