the reson for my query is: i am not able to understand overriding concept which states 'overriding methods should not throw new checked exceptions or broader exceptions whereas it can throw any new unchecked exception or narrower exceptions or no exceptions.
[ March 25, 2008: Message edited by: m prabhu ] [ March 25, 2008: Message edited by: m prabhu ]
Originally posted by m prabhu: 'overriding methods should not throw new checked exceptions or broader exceptions whereas it can throw any new unchecked exception or narrower exceptions or no exceptions.
This means that if a base class method throws an an exception, and when we override the same in the sub class (method signature should be same as per rule), we have the following choices
1. use the same exception in the overrided method 2. use a subclass of base class exception 3. or no exception 4. throw an unchecked (runtime) exception.
bcos as exception propagates up in the reverse order ( from subclasses to base..) we cant have a new checked exception or a more generalised exception than base class. [ March 25, 2008: Message edited by: Kesu sun ]
Joined: Jan 29, 2008
i can understand the overriding rule.
what i am not understanding is how to know -this is a unchecked exception and checked exception
Joined: Mar 19, 2008
Anything extending from RuntimeException class is an unchecked exception.
Joined: Mar 22, 2005
A checked exception extends java.lang.Exception. The Java compiler requires these to be declared to be thrown, or handled by a try/catch statement.
Unchecked exceptions extend java.lang.RuntimeException, and are not required to be handled by the code.
You can read the javadocs for the RuntimeException and Exception classes for more information on how to use each one.