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

Overriding methods & Exceptions

 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The overriding method rule clearly explain that the overriding method can declare only exceptions that are either the same class or a subclass of the exception.
if that is true, how do explain the following code?

the 'RuntimeException' class is NOT a subclass of 'Error' thus making the previous code illegal, but for a strange reason that is not the case (the code compiles without issuing any error). Anyone knows why?
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vicken Karaoghlanian:
The overriding method rule clearly explain that the overriding method can declare only exceptions that are either the same class or a subclass of the exception.
if that is true, how do explain the following code?

the 'RuntimeException' class is NOT a subclass of 'Error' thus making the previous code illegal, but for a strange reason that is not the case (the code compiles without issuing any error). Anyone knows why?


Hi Vicken,
Because they are unchecked exceptions.

JLS 8.4.4
A method that overrides or hides another method (�8.4.6), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please explain more about checked/unchecked exceptions?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The rules for overriding apply only to checked exceptions, you can declare any runtime exception you want. So you are correct with your statement that runtimeexception is NOT a subclass of error, but it is a runtime exception. When an error or a subclass of error is thrown, it's unchecked.
So, in conclusion, checked exceptions are all subtypes of Exception, excluding classes that extend RuntimeException.
Subtypes of Error and RuntimeExceptions are unchecked exceptions.
HTH
[ July 23, 2003: Message edited by: Andres Gonzalez ]
[ July 23, 2003: Message edited by: Andres Gonzalez ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Andres, that makes sence now...
two more questions:
1) the Exception class itself is unchecked, right?
2)is there any other checked/unchecked exception (other than the ones you've mentioned) that i should be aware of?
 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vicken,
NO, Exception class is a CHECKED Exception.
If a method A() declares throwing an Exception then the method calling A() should check for Exception either by catching it or declaring/throwing it.
Hope this clarifies.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i figured that earlier many thanks anywayz.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic