Effective Exception Handling in Java
Q: How do you handle exceptions in Java? What keywords are used for this purpose?
- Java Exception Handling
- Junior level question
Explore all the latest Java Exception Handling interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Java Exception Handling interview for FREE!
In Java, exceptions are handled using a combination of keywords including `try`, `catch`, `finally`, `throw`, and `throws`.
To handle exceptions, you typically wrap the code that may throw an exception within a `try` block. If an exception occurs, control is transferred to a corresponding `catch` block, where you can define how to handle the exception.
The `finally` block, if used, will execute after the `try` and `catch` blocks, regardless of whether an exception was thrown or not. This is useful for cleaning up resources, like closing files or network connections.
You can use the `throw` keyword to explicitly throw an exception from a method or block of code. If you want to declare that a method may throw an exception to its caller, you use the `throws` keyword in the method signature.
Here’s an example to illustrate:
```java
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int result = divide(10, 0);
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero: " + e.getMessage());
} finally {
System.out.println("Execution completed.");
}
}
public static int divide(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException("Division by zero");
}
return a / b;
}
}
```
In this example, the `divide` method throws an `ArithmeticException` if there is an attempt to divide by zero. The `try` block catches this exception and prints an appropriate message. The `finally` block is executed regardless of whether an exception occurs, indicating that the execution of the program is completed.
To handle exceptions, you typically wrap the code that may throw an exception within a `try` block. If an exception occurs, control is transferred to a corresponding `catch` block, where you can define how to handle the exception.
The `finally` block, if used, will execute after the `try` and `catch` blocks, regardless of whether an exception was thrown or not. This is useful for cleaning up resources, like closing files or network connections.
You can use the `throw` keyword to explicitly throw an exception from a method or block of code. If you want to declare that a method may throw an exception to its caller, you use the `throws` keyword in the method signature.
Here’s an example to illustrate:
```java
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int result = divide(10, 0);
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero: " + e.getMessage());
} finally {
System.out.println("Execution completed.");
}
}
public static int divide(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException("Division by zero");
}
return a / b;
}
}
```
In this example, the `divide` method throws an `ArithmeticException` if there is an attempt to divide by zero. The `try` block catches this exception and prints an appropriate message. The `finally` block is executed regardless of whether an exception occurs, indicating that the execution of the program is completed.


