Exception handling is an important part of any Java program. It allows us to gracefully handle errors and unexpected situations.
Table of Contents
Exception handling keywords
Java provides 5 exception handling keywords in Java. These are:
- try
- catch
- finally
- throw and
- throws
try
The try
keyword is used to enclose a block of code that may throw an exception. If an exception is thrown inside the try
block, the catch
block will be executed.
try {
// Code that may throw an exception
} catch (Exception e) {
// Handle the exception
}
catch
The catch
keyword is used to catch an exception that is thrown by the try
block. The catch
block must be followed by a parameter that specifies the type of exception that it can handle.
try {
// Code that may throw an exception
} catch (ArithmeticException e) {
// Handle the ArithmeticException exception
}
finally
The finally
keyword is used to execute a block of code regardless of whether or not an exception is thrown. The finally
block is often used to close resources, such as files and sockets.
try {
// Code that may throw an exception
} catch (Exception e) {
// Handle the exception
} finally {
// Close resources
}
Example to show try, catch, and finally keywords used together.
public class ExceptionHandlingDemo {
public static void main(String[] args) {
try {
int x = 10;
int y = 0;
// The following will throw an ArithmeticException exception
System.out.println(x / y);
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Closing resources");
}
}
}
When we run this code, it will print the following output:
Cannot divide by zero Closing resources
throw
The throw
keyword is used to throw an exception. The throw
keyword must be followed by an object of the Exception
class or a subclass of the Exception
class.
public void divideByZero() throws ArithmeticException {
int x = 10;
int y = 0;
// Throw an ArithmeticException exception if y is 0
if (y == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
}
throws
The throws
keyword is used to declare that a method may throw an exception. The throws
keyword must be followed by a list of the exceptions that the method may throw.
public int divide(int x, int y) throws ArithmeticException {
// Code that may throw an ArithmeticException exception
return x / y;
}
FAQs
What is the try
keyword used for in Java?
The try
keyword is used to enclose a block of code where exceptions may occur. It allows you to specify a portion of your code that needs error handling.
What is the purpose of the catch
keyword in Java?
The catch
keyword is used to define a block of code that handles exceptions. When an exception occurs in the associated try
block, the code in the catch
block is executed to handle the exception. And we can write our code that needs to be executed when this exception is occured.
How do I specify the type of exception to catch using the catch
keyword?
You specify the type of exception to catch by including the exception’s class in parentheses after the catch
keyword.
For example: catch (ExceptionType e)
{}.
What is the purpose of the throws
keyword in Java method declarations?
The throws
keyword is used in a method declaration to indicate that the method might throw a particular type of exception. It informs callers of the method about the exceptions they should handle or propagate.
Can you have multiple catch
blocks after a single try
block?
Yes, you can have multiple catch
blocks following a single try
block in Java. Each catch
block can handle a specific type of exception, allowing you to handle different exceptions differently.
When is the finally
block executed in Java’s exception handling?
The finally
block is executed regardless of whether an exception occurred or not in the associated try
block. It is the last block to execute in an exception-handling sequence.
What is the difference between throw
and throws
in Java?
throw
is used to manually throw an exception within our code, while throws
is used in method declarations to declare the types of exceptions that the method may throw.