Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is the hierachy significant in throws statement.i.e.is it illeagl to declare a method as

void method() throws Exception, IOException{
throw new IOException();
}
ofcourse i know tht there is no need to throw IOException in the above method . but i just want to know whether hierachy is significant in throws statement.

Thanks in advance.
vineela
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is a throws clause in the signature of a method, then that method can actually throw one of those:
  • No exceptions at all.
  • The same exception that is declared in the method signature.
  • Any exception that is a subset of the exception declared in the method's signature.

  •  
    Vineela Devi
    Ranch Hand
    Posts: 191
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Vicken,
    Thanks for the reply.I know the points u have specified.
    i wanted to know whether the code posted by me is illegal coz in some Certification book, it is mentioned tht the hierachy is significant in throws clause.
    vineela
     
    Sudhakar Krishnamurthy
    Ranch Hand
    Posts: 76
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nope, their is no hierarchy that you need to follow, your code should compile fine. But i hope you understand you would never have to do that since the base class exception is going to take precedence over the subclass exceptions when it is caught.
     
    Thomas De Vos
    stable boy
    Ranch Hand
    Posts: 425
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Additionally on the three bullet points mentioned in the posting from Vicken. The method can throw also unchecked exceptions like: NullPointerException, RuntimeException, etc.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic