Exception Hierarchy in Java
In this article we will discuss in detail about the Exception hierarchy in Java.
Why do we need exceptions in java?
As the name suggests exception refers to something which didn't go as expected . For example if a variable is expected to be an instance of a particular class but it actually is null then it will result in Null Pointer Exception ,if you try to call some method of that class.
At the top of the exception hierarchy there is a Throwable class. Thowable class extends Object class of Java. Any class which extends Throwable class can be thrown using the throw keyword of java.
Error:- Error are a type of exceptions which results in abrupt shutdown of the java program. For example , StackOverflowError or OutOfMemoryError.
Exceptions:- Exceptions can be handled properly in the code by developers for proper functioning of the program. Developers can use try,catch, throw and throws keywords for exception handling.
There are two types of exception:-
Checked Exceptions:- Checked exceptions are exceptions which are checked at the compile time.Java won't allow compilation of the code if proper exception handling is not done in the code.
For example:- IllegalArgumentException or I/O Exception.
public class A{
public void testIllegalArgumentException(int id) throws IllegalArgumentException{
if (id<0){
throw new IllegalArgumentException("id can't be negative");
}
else{
...................
}
}
}
public class B{
public void testA(){
A a = new A();
try{
a.testIllegalArgumentException(-1);
} catch(IllegalArgumentException e){
//Exception Handling
}
}
}
Unchecked Exceptions:- These are the exceptions which arise at runtime and can't be checked at compile time like NullPointerException.
I hope this article helped in clarifying the concepts of Exceptions in Java .In future articles,I will discuss in details about proper exception handling. For beginners , I am giving a link for a book to start with java in the comments.
Why do we need exceptions in java?
As the name suggests exception refers to something which didn't go as expected . For example if a variable is expected to be an instance of a particular class but it actually is null then it will result in Null Pointer Exception ,if you try to call some method of that class.
At the top of the exception hierarchy there is a Throwable class. Thowable class extends Object class of Java. Any class which extends Throwable class can be thrown using the throw keyword of java.
Error:- Error are a type of exceptions which results in abrupt shutdown of the java program. For example , StackOverflowError or OutOfMemoryError.
Exceptions:- Exceptions can be handled properly in the code by developers for proper functioning of the program. Developers can use try,catch, throw and throws keywords for exception handling.
There are two types of exception:-
Checked Exceptions:- Checked exceptions are exceptions which are checked at the compile time.Java won't allow compilation of the code if proper exception handling is not done in the code.
For example:- IllegalArgumentException or I/O Exception.
public class A{
public void testIllegalArgumentException(int id) throws IllegalArgumentException{
if (id<0){
throw new IllegalArgumentException("id can't be negative");
}
else{
...................
}
}
}
public class B{
public void testA(){
A a = new A();
try{
a.testIllegalArgumentException(-1);
} catch(IllegalArgumentException e){
//Exception Handling
}
}
}
Unchecked Exceptions:- These are the exceptions which arise at runtime and can't be checked at compile time like NullPointerException.
I hope this article helped in clarifying the concepts of Exceptions in Java .In future articles,I will discuss in details about proper exception handling. For beginners , I am giving a link for a book to start with java in the comments.
This comment has been removed by the author.
ReplyDeletehttps://amzn.to/2XfRa49
ReplyDelete