Skip to main content

Exception Handling in Java

  Handling the exception occurred during runtime or compile time is important to avoid user frustration. To handle these exceptions there are certain ways to do that.

By using Try and Catch blocks

Syntax

    try {
         -- Code that may cause exception --
    }
    catch(java.lang.Exception e) {
        -- Next line of the code that causes exception --
    }
    finally {
        -- Important code that should run whether the exception is handled or not -- 
    }

 

try - This keyword is used to put the code that may cause the exception, if the code in this block causes an exception, it will execute code in the catch block, or else code in the try block will get executed

catch - This block must be placed after the try block. If an exception occurred in the try block, the code in the catch block will get executed. catch block should be used along with try block and it should not be used alone.

finally - If the block is named with this keyword, that piece of code will get executed whether it got handled in the above block or not.

Using single catch block

Sample Program
---------------------------------------------------------------------------------------------------------------------
class Exception {
Scanner sc = new Scanner(System.in); 
static int age;
int age() {
return age = sc.nextInt();
}
public static void main(String[] args) {
Exception ex = new Exception();
Exception ex1 = new Exception();
try {
ex.age();
}
catch(java.lang.Exception e) {
System.out.println("Only numeric value is accepted.. Please reenter the value");
age=ex1.age();
}
finally {
System.out.println("Age is set as - "+age);
}
}
}
---------------------------------------------------------------------------------------------------------------------

Output:
Input: a
Only numeric value is accepted.. Please reenter the value
Input: 3
Age is set as - 3

    In the above program, if the user enters a value other than an integer, it will skip that step and the next line will get executed. This code is a small demonstration of getting an age. If the user enters an integer value it will display an integer value. If the user enters any alphabets, it will show the message "Only numeric value is accepted.. Please reenter the value" and asks the user to reenter the value. 

Multiple catch block

Sample Program
---------------------------------------------------------------------------------------------------------------------
class Exception {
static Scanner sc = new Scanner(System.in); 
static int[] age=new int[1];
int age() {
return age[0] = sc.nextInt();
}
public static void main(String[] args) {
Exception ex1 = new Exception();
try {
age[5] = ex1.age();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is not in range");
}
catch(java.lang.Exception e) {
System.out.println("String values are not allowed");
}
}
}
---------------------------------------------------------------------------------------------------------------------

Output:

Case 1
a9a9
String values are not allowed

Case 2
10
Array index is not in range

Using throw

throw - This keyword is used to throw checked exception or unchecked exception explicitly

Sample code piece

Input is given to the code = 10

if(a==10)
    throw new ArithmeticException("Value is not accepted");

While using the above piece, the output will show the exception as "java.lang.ArithmeticException: NotValid

Using throws

    This throws keyword is used to mention that exception may occur in this section. 
    Only checked exceptions will be handled and mostly unchecked exceptions were caused due to the programmer fault. 

Sample Program

---------------------------------------------------------------------------------------------------------------------
class Exception {
void check() throws ArithmeticException{
throw new ArithmeticException("Test Exception");
}
public static void main(String[] args) {
Exception ex1 = new Exception();
try {
ex1.check();
}
catch(ArithmeticException e) {
System.out.println("Flow continue");
}
}
}
---------------------------------------------------------------------------------------------------------------------

Output:
Flow continue

By using throws keyword, can able to mention the code that may cause exception and it helps programmers to know whether that needs to be handled in their implementation.

Comments