Exception handling is a powerful mechanism of Java to take care of the runtime errors without interrupting the normal flow of the program.
Are you interested in taking up Core Java Certification Training? Enroll Now for Core Java Training!
Table of Contents
An exception is an abnormal or unwanted condition that occurs during the program execution and disrupts the flow of the program.
The core advantage of exception handling is to maintain the flow of the program. Let us understand this by one example:
Consider a program is of 10 expressions like shown below:
Expr 1; Expr 2; Expr 3; Expr 4; Expr 5; Expr 6; //exception occurs Expr 7; Expr 8; Expr 9; Expr 10;
Now if there is an exception occurring at expression 6, the rest of the code after that will not be executed. In this case expr7 to 10 will be ignored. Now if we include exception handling in our code, rest of the expressions will be executed.
Exception: Exception occurs in the program which can be resolved and handled in program code. There are 2 types of exceptions available:
Error: Error cannot be resolved by a programmer. They are irrecoverable. Error is also one type of unchecked exception. They occur due to some scarcity of system resources. For Example JVM Error, Stack overflow, hardware error, etc.
Below 5 keywords are used to handle exceptions in Java.
Let us go through different examples of Exception Handing in Java.
public class DemoException{
public static void main(String args[]){
try
{
int d=10/0;
}
catch (ArithmeticException e)
{
System.out.println("Catch block"+ e);
}
System.out.println("rest of the code");
}
}
Output:
Catch block Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code
public class DemoException {
public static void main(String[] args) {
try
{
int a[]={4,8,9};
System.out.println(a[2]);
//a[3]=1;// if this line is uncommented then this statement will raise exception of type ArrayIndexOutobouncException. In this case control will got to that block which is handling this exception
int x=10;
System.out.println(x/0);
}
catch(ArithmeticException e1)
{
System.out.println("number cannot divided by zero"+e1);
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("array index out of bound exception"+e2);
}
catch(Exception e)
{
System.out.println(e);// when any matching exception is not handled then control will come to this block.
}
finally
{
System.out.println("in finally block"); //this will be executed even if exception is handled or not.
}
System.out.println("After try catch finally block");
}
}
Output:
9
number cannot divided by zerojava.lang.ArithmeticException: / by zero
in finally block
After try catch block
public class throwDemo{
public static void main(String[] args) {
throw new ArithmeticException("throwing arithmetic exception");
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: throwing arithmetic exception at throwDemo.main(throwDemo.java:4)
public class ThrowsExceptionDemo {
public static void main(String[] args) throws FileNotFoundException {
openF("G: est.txt");
}
public static void openF(String fname) throws FileNotFoundException{
FileInputStream f= new FileInputStream(fname);
}
}
Java has some in-built in exception classes inside the standard package java.lang. You might have faced some of those exceptions till now. These unchecked exceptions must be included in any method’s throw list if that method is supposed to trigger that exception and it is not handled there.
Below is the list of in-built unchecked and checked exceptions provided by Java with its meaning.
Unchecked Exception | Meaning |
ArithmeticException | Arithmetic error, such as divide-by-zero. |
ArrayIndexOutOfBoundsException | Array index is out-of-bounds |
IllegalArgumentException | Illegal argument used to invoke a method. |
IndexOutOfBoundsException | Some type of index is out-of-bounds. |
NullPointerException | Invalid use of a null reference. |
NumberFormatException | Invalid conversion of a string to a numeric format. |
StringIndexOutOfBounds | Attempt to index outside the bounds of a string. |
TypeNotPresentException | Type not found. |
Checked Exception | Meaning |
ClassNotFoundException | Class not found |
IllegalAccessException | Access to a class is denied |
InstantiationException | Attempt to create an object of an abstract class or interface. |
InterruptedException | One thread has been interrupted by another thread |
NoSuchFieldException | A requested field does not exist. |
NoSuchMethodException | A requested method does not exist. |
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Core Java Training | Nov 19 to Dec 04 | View Details |
Core Java Training | Nov 23 to Dec 08 | View Details |
Core Java Training | Nov 26 to Dec 11 | View Details |
Core Java Training | Nov 30 to Dec 15 | View Details |
Ravindra Savaram is a Technical Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.