• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding a method with Different Exceptions

 
Pavan Chillara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have written a class which would override a method in a super class,But super class method is throwing an ArithmeticException and method in sub class which will be overriding the above throwing an exception by name Exception.When i try to execute this it will be giving compilation error,But viceversa does not,
Can anyone explain the reason for this,The code is as follows
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint:

ArithmeticException is a subclass of Exception and to be mroe precise it is a subclass of RuntimeException.


pascal
[ October 30, 2006: Message edited by: pascal betz ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you override a method, you can declare the same exceptions, or some of them, or none of them; or you can declare subclasses of some or all of them. In other words, a subclass method can take away things from the superclass method's exception declarations, but it can't add anything. Your only choice is to catch all exceptions inside your subclass method and rethrow them as unchecked exceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic