• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the error message from the compiler carefully.

What do you think it is trying to tell you?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just double check that you have not mistakenly commented the below statement which is the cause for the error.
 
reply
    Bookmark Topic Watch Topic
  • New Topic