(1) checked exception: those are verified during compile time.
(2) unchecked exception: those are not verified during compile time.
(3) error (unchecked)
(1): A Checked exception can be predicted by the programmer.
(2): A Unchecked exception is a runtime exception. it may not be predicted as coding it, fx: index out of bound etc.
(3): Errors made by JVM, for instance, run out of memory etc.
An exception extends a Throwable interface. All exceptions are throwables.
(1): extends from Exception extends Throwable; fx: IOException
(2): extends from RuntimeException extends Throwable; for instance: ArithmeticException
(3): extends from Error extends Throwable
(1) must be handled by a developer by try catch finally, or throws to a caller.
(2) not an error; a developer may handle it or not; whatever it will be handled by the system.
(3) Errors come from the system; they cannot be handled.
Rule in inheritance
class A{
doIt(){ }; //overriden method
}
class B extends A{
@override
doIt() throws RuntimeException{ //overriding method
}
}
In general, an overriding method cannot throw an exception more generic than the overridden method. The case above is valid, only because a RuntimeException is an unchecked exception, so the overriding is valid.
No comments:
Post a Comment