Skip to main content

Throwing and Catching Exceptions in Java

Introduction​

In Java, exceptions are thrown to indicate exceptional conditions that may occur during the execution of a program. Exceptions can be thrown explicitly using the throw keyword or can be thrown implicitly by the Java runtime system.

Throwing Exceptions​

Syntax​

Exceptions can be thrown explicitly using the throw keyword followed by an instance of the exception class.

throw new ExceptionType("Error message");

Example​

public class ThrowExceptionExample {
public static void main(String[] args) {
int age = -5;
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
}
}

Catching Exceptions​

Syntax​

Exceptions are caught using the try-catch block. The try block contains the code that may throw an exception, and the catch block handles the exception.

try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
}

Example​

public class CatchExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0; // This will throw ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
}
}

Multiple Catch Blocks​

You can have multiple catch blocks to handle different types of exceptions.

Example​

public class MultipleCatchExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]); // This will throw ArrayIndexOutOfBoundsException
int result = 10 / 0; // This will throw ArithmeticException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is out of bounds");
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
}
}

Finally Block​

The finally block is used to execute important code such as closing resources, regardless of whether an exception is thrown or not.

Syntax​

try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
} finally {
// Code to be executed regardless of an exception
}

Example​

public class FinallyExample {
public static void main(String[] args) {
try {
int result = 10 / 0; // This will throw ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("This is the finally block");
}
}
}

Try-With-Resources​

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement.

Syntax​

try (ResourceType resource = new ResourceType()) {
// Use the resource
} catch (ExceptionType e) {
// Code to handle the exception
}

Example​

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TryWithResourcesExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("File not found or unable to read the file");
}
}
}

Conclusion​

Throwing and catching exceptions are essential aspects of Java programming, allowing you to handle unexpected conditions and errors gracefully. By understanding how to throw and catch exceptions, as well as how to use the finally block and try-with-resources statement, you can write more robust and reliable Java code.