aspose file tools
The moose likes Beginning Java and the fly likes Overriding a method with Different Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Overriding a method with Different Exceptions" Watch "Overriding a method with Different Exceptions" New topic
Author

Overriding a method with Different Exceptions

Pavan Chillara
Greenhorn

Joined: Sep 27, 2006
Posts: 14
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

Joined: Jun 19, 2001
Posts: 547
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

Joined: Jul 08, 2003
Posts: 24061
    
  13

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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Overriding a method with Different Exceptions
 
Similar Threads
How do you treat a Real data type as a float
overriding JDBCRealm
Exercise on Propagating Exceptions. Am I missing something?
Instance of an abstract class
Want to Edit cells in a JTable