Exceptions

 Exception is an event that occurs during an execution of a program that disrupts its normal flow. When such unexpected behaviour occurs, an exception object is created to represent this. Exceptions need to be handled by the application.

Errors are not recoverable such as Java Virtual Machine running out of memory, memory leak, stack overflow etc. Application should not try to handle this.

Hierarchy Of Exception

All Exceptions and Errors are subclasses of class Throwable. Java defines several types of exceptions that are available in libraries. Those are build-in exceptions which is again divided into checked exception and unchecked exception.

Checked exception are exceptions that are checked by the compiler at compile time.
Eg: IOException, ClassNotFoundException, FileNotFoundException, SQLException etc.

Unchecked exceptions occur at run time and the compiler will not check those exceptions at compile time.
Eg: NullPointerException, ArithmeticException, ArrayIndexOutOfBoundException etc.

Java also supports the creation of custom exception classes by extending the Exception class or one of its subclasses. This are called user defined exceptions which allows developers to define and handle application-specific exception types.

CustomException.java

public class CustomException extends Exception {
// Constructors
public MyCustomException() {
super();
}

public MyCustomException(String message) {
super(message);
}

public MyCustomException(String message, Throwable cause) {
super(message, cause);
}
}


Exception Handling: Java provides a mechanism to handle the exception.
  • In a try-catch finally block, the code that might throw an exception is enclosed in a try block, and the handling code is written in the corresponding catch block. Code within a finally block is always executed, regardless of whether an exception occurs or not. This is useful for releasing resources or performing cleanup tasks. Within a try block there can occur multiple exceptions. Multiple exception handlers or catch blocks are need to handle this.
  • The throws keyword is used in a method declaration to specify that the method might throw certain exceptions, and the caller of the method must handle those exceptions.
  • The throw keyword is used to explicitly throw an exception from a method or a block of code.
UserClass.java

public class UserClass {
public void performOperation() throws CustomException {
// Perform some operation
// If an error condition is detected, throw the custom exception
throw new CustomException("An error occurred during the operation");
}
}

MainClass.java

public class MainClass {
public void execute() {
UserClass userClass = new UserClass();
try {
userClass.performOperation();
} catch (CustomException e) {
// Handle the custom exception
System.out.println("Custom exception caught: " + e.getMessage());
} finally {
            // any clean up code comes here
        }
}
}


Comments

Popular posts from this blog

Luhn Algorithm

JWT (JSON Web Token)

Security in General