Exception handling

This is the most significant feature of Java

An exception is defined as an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions.

Whenever an exception occurs a runtime time object is created which is called an exception object  which contains information like type and the state of the program when the error occurred

Creating an exception object and handing it to the runtime system is called throwing an exception

Whenever an exception occurs a runtime time object searches for a block of code to handle it which is called an exception handler  which will catch the exception

Exceptions are used to indicate many different types of error conditions which can be categorized as follows.

 

JVM Errors:

 

- Out Of Memory Error

- Stack Overflow Error

- Linkage Error

 

System errors:

 

- File Not Found Exception

- IO Exception

- Socket Time out Exception

 

Programming errors:

- Null Pointer Exception

- Array Index Out Of Bounds Exception

- Arithmetic Exception

 

Advantages of exception handling

 

- Exceptions separate error-handling code from regular code

- Exceptions propagate errors up the call stack.

- Exception classes group and differentiate error types.

- Exceptions standardize error handling.

 

Keywords in Java exception

 

try

 

try statement should be specified in the block where the exception needs to be handled. It is followed by catch or finally which means try cannot be used alone

 

catch

 

catch block is used to handle the exception. It must be preceded by a try block which means we can't use a catch block alone. It can be followed by final block later.

 

Finally

 

Finally, the block is used to execute the important code of the program. It is executed whether an exception is handled or not.

 

Throw

 

The throw keyword is used to throw an exception.

 

Types of exception

 

Checked exception

 

The classes that directly inherit the Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException, etc. Checked exceptions are checked at compile-time so this needs to be handled which can be just logging the exception or returning an error message to the user

 

Unchecked exception

 

The classes that inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime and this can be ignored sometimes

 

Error

Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

Simple usecase

public class JavaExceptionSample {
    public static void main(String args[]) {
        try {
            int x = 100 / 0;
        }
        catch (ArithmeticException e) {
            System.out.println(e);
        }
        System.out.println("rest of the code...");
    }
}

 

Hints to understand the basic exception categories

 

Null Pointer Exception

 

The parameter value is null while performing an operation

 

Arithmetic Exception

 

If we perform operations like division by zero

 

Number Format Exception

 

This occurs when a string value is assigned to an integer datatype variable

 

Array Index Out Of Bounds Exception

 

This occurs when you are inserting any value in the wrong index

 

Illegal Argument Exception

 

Non-null parameter value is inappropriate

 

Illegal State Exception

 

Object state is inappropriate for method invocation

 

Index Out Of Bounds Exception

 

The index parameter is out of range

 

Unsupported Operation Exception

 

The object does not support the method

 

 

 

Related Tutorials

Map