Exception handling in Java is a mechanism that handles runtime errors, preventing program termination and allowing the program to continue its execution. This section covers the basic concepts and usage of exception handling in Java.
1. Try, Catch, Finally
The try
block allows you to test a block of code for exceptions. The catch
block handles the exception, and the finally
block executes code regardless of whether an exception occurred or not.
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int result = 10 / 0; // This will throw an ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Caught an exception: " + e.getMessage());
} finally {
System.out.println("This block always executes, even if an exception occurs.");
}
}
}
2. Throw and Throws
The throw
keyword is used to explicitly throw an exception. The throws
keyword is used in method signatures to declare that a method may throw one or more exceptions.
public class ThrowThrowsExample {
// This method declares that it throws an exception
public static void divide(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
} else {
System.out.println("Result: " + (a / b));
}
}
public static void main(String[] args) {
try {
divide(10, 0);
} catch (ArithmeticException e) {
System.out.println("Caught exception: " + e.getMessage());
}
}
}
3. Custom Exceptions
Custom exceptions allow you to create your own exception classes to handle specific errors that are relevant to your application. A custom exception must extend the Exception
class (for checked exceptions) or the RuntimeException
class (for unchecked exceptions).
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
public class CustomExceptionExample {
// Method to check age
public static void checkAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be at least 18.");
} else {
System.out.println("Age is valid.");
}
}
public static void main(String[] args) {
try {
checkAge(16);
} catch (InvalidAgeException e) {
System.out.println("Caught custom exception: " + e.getMessage());
}
}
}
4. Exception Hierarchy (Checked vs Unchecked Exceptions)
Java exceptions are divided into two categories:
- Checked Exceptions: These are exceptions that are checked at compile time. For example,
IOException
andSQLException
. - Unchecked Exceptions: These are exceptions that are not checked at compile time and occur during runtime. For example,
NullPointerException
,ArrayIndexOutOfBoundsException
.
public class ExceptionHierarchyExample {
public static void main(String[] args) {
// Unchecked Exception (Runtime)
try {
String str = null;
System.out.println(str.length()); // NullPointerException
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException: " + e.getMessage());
}
// Checked Exception (Compile-time)
try {
Thread.sleep(1000); // InterruptedException
} catch (InterruptedException e) {
System.out.println("Caught InterruptedException: " + e.getMessage());
}
}
}
Conclusion
Mastering exception handling in Java is essential for building robust and error-resistant Selenium automation scripts. Proper exception handling ensures that tests continue to run even when unexpected errors occur, helping identify issues in test automation and improving stability.