• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding a method with Different Exceptions

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic