What is an Exception and How to do Exception Handling in Java

Exception and Exception Handling in Java

1)Exception:

An exception is an unnecessary or unintended occurrence which occurs during program execution, i.e. during runtime, and disrupts the normal  flow of instructions of the program.

public class errorExample {
    public static void main(String[] args)
    {
        String givenString = null;
        // calculating  the length of string
        int string_length = givenString.length();

        System.out.println("length of string = "
                           + string_length);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException
	at GFG.main(File.java:9)

Explanation:

Because the String reference givenString was set to null and a member function was called with it, this is the case.
As a result, a runtime error occurred, and a NullPointerException was thrown. However, this code failed to handle
the triggered exception, and the application crashed as a result.

2)Error vs Exception:

Error:

An error suggests that a serious problem should not be resolved by a reasonable request.

Exception:

 An exception denotes a set of circumstances that a reasonable application would attempt to catch.

3)Exception Handling in Java:

If an exception occurs, an Exception class object is created and Java Runtime looks for its operator.

The code we want to deal with in the try block has to be the exception

try
{
...........
.......
...........
.........
.........
..........
}

The exception in the try block should now be handled by a catch block

catch(Exception ex)
{
...........
.........
.......
.......
........
.........
}

When an exception occurs, the Runtime will look for its Exception Handler, which is typically a catch block. First and foremost, it will look for an exception in the current function.

4)Exception handling when the length of string is null

Let’s look at how we can catch the exception in the preceding example by enclosing the code in a try block and handling the exception in a catch block.

NullPointerException was caught in the same function and handled appropriately. The execution flow of the programme will move directly to the catch block from the line where the exception is triggered. It means that lines following that will not be executed, as shown below.

Below is the implementation:

import java.io.*;

class BtechGeeks {
    public static void main(String[] args)
    {
        // declaring length of string as 0
        int string_length = 0;
        try {
            // taking a string with null value
            String givenString = null;
            // calculating  the length of string
            string_length = givenString.length();
        }
        catch (NullPointerException e) {
            System.out.println(
                "It is a Null Pointer Exception ");
        }
        // printing the length of string
        System.out.println("length of string = "
                           + string_length);
    }
}

Output:

It is a Null Pointer Exception 
length of string = 0

5)Exception Handling in Parent Functions

The code that caused the exception was in the try block, and the catch block was also in the same function. But what if there is a suitable handler in the function where the exception occurred?

If the Java Runtime does not find a handler in the function where the exception occurred, it will search in its parent function from the call stack until it finds a handler or the main function is reached. If it reaches the main() function and still finds no handler, the application will crash.

Let’s look at how to handle exceptions in the parent function.

Below is the implementation:

import java.io.*;

class BTechGeeks {
    public static void checkString()
    {
        // taking a string with null value
        String givenString = null;
        // calculating  the length of string
        int string_length = givenString.length();
        // printing the length of string
        System.out.println("length of string = "
                           + string_length);
    }
    public static void main(String[] args)
    {

        try {
            BTechGeeks.checkString();
        }
        catch (NullPointerException e) {
            System.out.println(
                "It is a Null Pointer Exception ");
        }

        System.out.println("Exiting from main");
    }
}

Output:

It is a Null Pointer Exception 
Exiting from main

 
Related Programs: