Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Why is this illegal?

 
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface MyInterface {
   void aMethod() throws java.io.IOException;
}

public class SampleClass implements MyInterface {

   public void aMethod() throws IOException {}                         // WHY IS THIS ILLEGAL? - COMPILATION ERROR
}
 
Marshal
Posts: 3461
494
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What specifically is the error?
 
Clemonte Johnstone
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:What specifically is the error?



Overriden method does not support this exception
 
Ron McLeod
Marshal
Posts: 3461
494
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the interface, the exception for the method is is fully qualified: throws java.io.IOException
In the implementation, the exception it is not: throws IOException

Did you have an import statement for java.io.IOException?
 
Clemonte Johnstone
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:In the interface, the exception for the method is is fully qualified: throws java.io.IOException
In the implementation, the exception it is not: throws IOException

Did you have an import statement for java.io.IOException?



Yes I did. It still showed the same compilation error. So what's the conclusion?
 
Ron McLeod
Marshal
Posts: 3461
494
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:Yes I did. It still showed the same compilation error. So what's the conclusion?


I'm not sure - I don't see an error when I compile your code using Java 8:
Are you seeing the error at compile time, or at run time?
 
Master Rancher
Posts: 4256
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the complete code that gets the error (with all the imports)
The posted code compiles for me with required import statement.
 
Bartender
Posts: 3699
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clemonte, may I guess the compiler complains because of the empty body?
Can you add inside the curly braces "throws new IOException();" and see if it helps?

UPD: I mean "throw", not "throws" 8(((
 
Mikalai Zaikin
Bartender
Posts: 3699
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another option is that SampleClass refers some "custom" IOException from a different package.

It's apparently a bad practice (compare: java.util.Date v.s. java.sql.Date), but might it happen that SampleClass uses (and imports) some com.mycompany.IOException, and MyInterface refers by fully qualified name java.io.IOException?
 
Clemonte Johnstone
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mikalai Zaikin wrote:Clemonte, may I guess the compiler complains because of the empty body?
Can you add inside the curly braces "throws new IOException();" and see if it helps?



 
Clemonte Johnstone
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stange now its working all of a sudden
 
Mikalai Zaikin
Bartender
Posts: 3699
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:

Mikalai Zaikin wrote:Clemonte, may I guess the compiler complains because of the empty body?
Can you add inside the curly braces "throws new IOException();" and see if it helps?





This code absolutely correct. You may not throw in sub-classes or implementing classes more "wide" (or more "generic") exceptions.
1. You may throw more "narrow" exceptions
2. You may just not throw anything (missing "throws" section

SIDENOTE: I wrote incorrectly above, I mean "throw", not "throws" inside curly braces block 8(
 
Marshal
Posts: 72487
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I shall have to refer you to the JLS (=Java┬« Language Specification), but doesn't a fully‑implemented method have to show some possible path of execution that will throw the declared checked exception? I think there is an exemption for Exception itself.
 
Master Rancher
Posts: 3833
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I shall have to refer you to the JLS (=Java┬« Language Specification), but doesn't a fully‑implemented method have to show some possible path of execution that will throw the declared checked exception? I think there is an exemption for Exception itself.



I was having a similar memory.  But I believe we're both thinking of the fact that if you have a catch block for a checked exception, the compiler must be able to verify that it was possible for that checked exception to be thrown from the try block.  With rule exceptions for Exception and Throwable, which can be supertypes of both checked and unchecked exceptions.  But for a method throws clause, there is no similar rule.  I believe the reason for this is that we may want a method to be overrideable, and the overrides might need to throw a given exception even if the base implementation does not.  However, even a private method does not require checked exceptions in the throws clause to be throwable from the body of the method.  This may just be an oversight in the rules.  
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic