Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Exception Handling

In Java, at the time of program execution, we generally get unexpected situations called exceptions, and as responsible developers, we must handle all the possible exceptions.
Before handling the exception, we must understand what is the actual meaning of Exception Handling.
Exception Handling is a process to provide an implementation or write a code that will execute when any exception occurs in the monitored code.
In Java either we can handle the exception using try-catch block or using throws keyword we transfer the responsibility to handle that exception to the caller of the method.

Try-Catch block:

In Java there are two keywords try & catch which is dedicated to handle an exception.

Syntax:-

try {
// Here we enclosed all the codes which we need to monitored.
} catch (Exception e) {
// Here we all the alternate code which executes when we get an exception in monitories code or try-block code.
}

Internal working of try-catch block:

Whenever any problem is identified, the following things will be done internally by the JVM:

1. If any problem or exception is identified in the try-block code, control will be immediately transferred to the JVM.
2. Now, JVM will identify the exact type of problem or exception.
3. After identifying the exact type of problem, it will create an object for that and throw it to the program.
4. In the program, it will try to search for matching catch blocks.
5. If the matching catch block is found, then the corresponding catch block will be processed.

            
class JTC{
	public static void main(String arg[]){
		try{
			int a = 10;
			int b = 0;
			int c = a / b; // Line-5
			System.out.println("c :- "+c);
		}catch(ArithmeticException e){
			System.out.println("Arithmetic Exception occurred");
		}
	}
}
    Arithmetic Exception occurred
            

How to handle the exception using multiple catch blocks:

1. Enclose that piece of code that you want to monitor inside the try block.
2. Write the catch block associated with that try with the proper exception type.
3. By using a catch, you can handle only one exception at a time.
4. You can write multiple catch statements associated with a try to handle the multiple types of exceptions.

While writing multiple try and catch remember the following points:

1. There should not be any statement between try and catch.
2. There should not be any statement between catch and catch.
3. You can write try without any catch but you cannot write catch without any try.
4. When you are writing the catch blocks, the catch parameter should be sub to super type. It cannot have parameter from super to sub type.

            
class JTC{
	public static void main(String arg[]){
		try{
			int a = Integer.parseInt(arg[0]); // Line-1
			int b = Integer.parseInt(arg[1]); // Line-2
			int c = a / b; // Line-3
			System.out.println("c :- "+c);
		}catch(ArithmeticException e){
			System.out.println("Arithmetic Exception occured...!");
			System.out.println("Please enter the non-zero value as b");
		}catch(NumberFormatException e){
			System.out.println("Number Format Exception occured...!");
			System.out.println("Please Enter the input in number format.");
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println("ArrayIndexOutOfBoundsExceptionoccured...!");
			System.out.println("Please Enter the 2 input from cmd.");
		}catch(Exception e){
			System.out.println("This cathc block executes when we get an exception other then above mentioned exception...!");
		}

	}
}
    C:\Users\Rahul\Desktop>java JTC
    ArrayIndexOutOfBoundsExceptionoccured...!
    Please Enter the 2 input from cmd.

    C:\Users\Rahul\Desktop>java JTC 10 0
    Arithmetic Exception occured...!
    Please enter the non-zero value as b

    C:\Users\Rahul\Desktop>java JTC abc 2
    Number Format Exception occured...!
    Please Enter the input in number format.

    C:\Users\Rahul\Desktop>java JTC 10 2
    c :- 5
            

In the above example as we can see we have 4 different catch-blocks along with ArithmeticException, NumberFormatException, ArrayIndexOutOfBoundsException and Exception respectively which are associated with same try-block.

In the output screen as we can see in first execution we are not providing any input from the command prompt, so in this case we get ArrayIndexOutOfBoundsException in Line-1 try-block code and JVM is finding and executing corresponding catch block as per their internal working culture which we have discussed earlier in the same tutorial, and same thing is happening for second execution where we are providing 0 as the value of b and getting Arithmetic Exception and in third execution providing abc as the value of a and getting NumberFormatException.