jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Overriding a method with Different Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overriding a method with Different Exceptions" Watch "Overriding a method with Different Exceptions" New topic

Overriding a method with Different Exceptions

Pavan Chillara

Joined: Sep 27, 2006
Posts: 14

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

Joined: Jun 19, 2001
Posts: 547

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

[ October 30, 2006: Message edited by: pascal betz ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

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.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link: http://aspose.com/file-tools
subject: Overriding a method with Different Exceptions
It's not a secret anymore!