aspose file tools*
The moose likes Beginning Java and the fly likes exception java.lang.InterruptedException is never thrown in body of corresponding try statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exception java.lang.InterruptedException is never thrown in body of corresponding try statement" Watch "exception java.lang.InterruptedException is never thrown in body of corresponding try statement" New topic
Author

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

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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)


Jon
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Read the error message from the compiler carefully.

What do you think it is trying to tell you?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Were you able to figure out the source of error in the below code?
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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
Bartender

Joined: May 31, 2011
Posts: 1776
just double check that you have not mistakenly commented the below statement which is the cause for the error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exception java.lang.InterruptedException is never thrown in body of corresponding try statement