Exception handling in Java Overview

Exception handling in Java

We do not like exceptions, but we always have to deal with them, great news is that the exception handling in Java is very robust and easy to understand and use. Java exceptions can arise from different types of situations, such as erroneous data inserted by the user, hardware failure, network connection failures, inoperable database server, etc. In this section, we will learn how exceptions are discussed in Java.

Java is an object-oriented programming language, whenever an error occurs during the execution of an instruction, creates an exception object, and then the normal flow of the program stops and the JRE tries to locate someone who can manipulate the exception generated. The exception object contains a lot of debug information, such as method hierarchy, line number in which the exception occurred, type of exception, etc. When the exception occurs in a method, the process of creating the exception object and delivery to the runtime environment is called “Play the exception.”

When the execution time receives the exception object, it tries to locate the exception handler. The Exception Handler is the block of code that the exception object can process. The logic to find the exception handler is simple – by starting the search in the method where the error occurred, if no appropriate driver is found, then go to the calling method and so on. Therefore, if the call stack methods are A-> B-> C and the exception is raised in method C, the search for the appropriate driver will move from C-> B-> A. If the driver is found for appropriate exceptions, the exception object will be passed to the controller to process it. The manipulator is said to be “taking the exception”. If there is no appropriate exception handler, the program closes the print information about the exception.

Note that the Java Exception handling is a structure used to treat only runtime errors, compile-time errors are not addressed by the treatment of exceptions in java.

We use specific keywords in the java program to create an exception handler block, we will see these keywords below.

Java Exception Handling Keywords

Java provides specific keywords for the treatment of exceptions, we will take care of them first and then we will write a simple program showing how to use them for the treatment of exceptions.

throw– We know that if an exception occurs, an exception object will be created and then the Java Runtime will initiate processing to deal with them. Sometimes we may want to generate an exception explicitly in our code, for example, in a user authentication program, we must throw an exception to the client if the password is null. The shooting keyword is used to throw an exception to the execution time to handle this.

throws – When we are throwing some exception in a method and not manipulating it, then we need to use keyword throws in the signature of the method to allow the calling program to know the exceptions that can be thrown by the method. The caller method can manipulate these exceptions or propagate it to the caller method using the keyword throws. We can provide several exceptions in the throws clause and can also be used with the main () method.

try-catch – We use the try-catch block to treat exceptions in our code. try is the beginning of the block and the catch is at the end of the try block to manipulate the exceptions. We can have several catch blocks with a try and the try-catch block can also be nested. The catch block requires a parameter that must be of type Exception.

finally – finally the block is optional and can be used only with the try-catch block. Since the exception interrupts the execution process, we can have some open resources that will not be closed, so we can use the finally block. finally the block is always executed, regardless of whether the exception has occurred or not.

We are going to see a simple program showing the exceptions handling in java.

package com.journaldev.exceptions;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionHandling {
public static void main (String [] args) launches FileNotFoundException, IOException {
try {
testException (-5);
testException (-10);
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
Finally} {
System.out.println ("Release resources");
testException (15);
public static void testException (int i) throws FileNotFoundException, IOException {
if (i <0) {
FileNotFoundException myException = new FileNotFoundException ("Negative integer" + i);
throw myException;
}} else if (i> 10) {
throw new iOException ("Only supported by index 0 to 10");

The output of the previous program is:

java.io.FileNotFoundException: Integer Negative -5
in com.journaldev.exceptions.ExceptionHandling.testException (ExceptionHandling.java:24)
in com.journaldev.exceptions.ExceptionHandling.main (ExceptionHandling.java:10)
Releasing resources
Exception in the "main" thread java.io.IOException: only supported for the index from 0 to 10
in com.journaldev.exceptions.ExceptionHandling.testException (ExceptionHandling.java:27)
in com.journaldev.exceptions.ExceptionHandling.main (ExceptionHandling.java:19)

Interested in learning Java? Enroll now:” Java training in Chennai “

Note that the testException () method is throwing the exception using the keyword throw and the signature applications of the throws keyword method to allow the caller to know the type of exceptions it can throw. In the main () method, I’m manipulating the exception using the try-catch block in the main () method and when I’m not manipulating, I’m spreading to runtime with throws clause in main method. Note that testException (-10) is never executed because of the exception, and then execution of the finally block after execution of the try-catch block. PrintStackTrace () is one of the useful methods in the Exception class and is used for debugging purposes.

  • We cannot have a catch or finally clause without a try statement.
  • A try statement must have a catch block or finally block, it can have both blocks.
  • We cannot write any code between the try-catch-finally block.
  • We can have several catch blocks with a single try statement.
  • Try-catch blocks can be nested similarly to the if-else statements.
  • We can have only one final block with a try-catch statement.

To getting expert-level training for Java Training in your location – java training in Chennai | java training in Bangalore  | java training in Pune | java training in Chennai | java training in Bangalore | java training in Chennai | java interview questions and answers |  core java interview questions and answers | java training in Chennai | For getting java online training | java online training