What is Exception:
Excetion is a runtime error caused due to logical mistake occured in the program beacause of wrong input given by the User.
- According to Java language, value, message or excpetion everything is an object.
- In function programming, we return value.
- Throwing exception is like returning value and If we see according to User,methods are just services nothing else.
What is our(developer's) responsibility:
We must convert Java Exception message into End User Understandable.Here, Conversion I mean,catching Exception object and get information using getters provided such as getMessage() and modify this message with End user understandable format.
Sun Microsystem(now Oracle) defined several classes to represent and handle logical mistakes.
All these classes are defined in java.lang package with a super class Throwable.
All Exception handling classes categorized into two sub-categories:
- Error
- Exception
Exception classes are again divided into two categories (These are not class names):
- Runtime Exceptions
- Checked Exceptions
So according to that all exception handling classes are divided into three categories:
- Errors
- Runtime Exceptions
- Checked Exceptions
- All sub-classes of java.lang.Error are under Error category
- All sub-classes of java.lang.RuntimeException are under Runtime Exception category.
- java.lang.RuntimeException is a sub-class of java.lang.Exception class and remaining all subclasses are checked Exceptions.
So according to above diagram,Errors and Runtime Exceptions are called as unchecked exceptions
Error, RuntimeException and their subclasses are called Unchecked Exceptions because their exception handling is not checked by the compiler as they are intended to raise and throw at runtime not by Developer.
Even if they are raised by Developer Compiler will not take note of it and doesn't check their handling.
Following are some of the examles of Error which are raised only at runtime due to problem occured in JVM:
- StackOverflowError
- OutOfMemoryError
- NoClassDefFoundError
- NoSuchMethodFoundError
Following are the examles of Runtime Exceptions which are raised only at runtime due to operation execution failure:
- ArithmeticException
- ArrayIndexOutOfBoundException
- NullPointerException
- ClassCastException
Throwable, Exception and its direct sub-classes are called as checked exceptions because this exceptions handling is checked by compiler cause these exceptions are thrown explicitly by developer
Rule:
If developer is throwing checked exception then those exceptions must be reported to its method caller else compiler throws compile time error saying
"Unreported exception must be caught or declared to be thrown"
How can developer throw and report exception?
Using throw keyword, Developer can throw exception and using throws keyword can report an exception
Syntax:
class Example{
void m1() throws <exception_class_name>{
throw <exception_class_object>;
}
}
We use throw and throws keyword for raising or throwing and reporting checked exception,, we can also use them for throwing unchecked exception
Rule:
throws keyword is mondatory to report checked exception, it is optional for unchecked exception.
Example:
class Example{
void m1() throws ClassNotFoundException{
throw new ClassNotFoundException();
}
public static void main(String args[]){
Example example = new Examplle();
example.m1();
}
}
Comments
Post a Comment