In addition to simply handling Java's exceptions, you can also throw your own exception. Here is an example of using the
throw keyword. If you are performing a calculation that might involve some division, you might wish to perform a check to see if the divisor is zero (0), and if it is, throw an exception, as in:
double calcResult(double divisor, double dividend)
throws ArithmeticException {
if (divisor == 0)
throw new ArithmeticException();
else
return dividend/divisor;
}
Notice how we must make a new instance of the appropriate class and throw that instance. What this code does is create a method for dividing double values that throws an exception if the divisor is zero (0). (Normally, operations involving double values never throw an exception, not even when executing something like 1.0/0.0.)
In Java, you can create and throw your own custom exceptions to handle specific scenarios or to provide more meaningful error messages than the standard runtime exceptions. Here's an example of how you might do this in Java SE 21:
Step 1: Define Your Custom Exception
First, you will define a custom exception class. This class should extend RuntimeException if you want it to be an unchecked exception (which doesn't need to be declared or caught explicitly), or extend Exception if you want it to be a checked exception.
public class CustomRuntimeException extends RuntimeException {
// Constructor without a message
public CustomRuntimeException() {
super();
}
// Constructor with a message
public CustomRuntimeException(String message) {
super(message);
}
// Constructor with a message and cause
public CustomRuntimeException(String message, Throwable cause) {
super(message, cause);
}
// Constructor with just a cause
public CustomRuntimeException(Throwable cause) {
super(cause);
}
}
Step 2: Use Your Custom Exception
Now, you can use this custom exception in your code. Here's an example where we might throw this exception in a method that performs some operation:
public class ExampleService {
public void performOperation(String input) {
if (input == null || input.isEmpty()) {
// Throw our custom exception with a descriptive message
throw new CustomRuntimeException("Input cannot be null or empty");
}
// Perform some operation with the input
System.out.println("Operation performed on: " + input);
}
}
Step 3: Handling the Exception
Since CustomRuntimeException extends RuntimeException, you don't need to handle it explicitly, but you might choose to do so for specific reasons:
public class Main {
public static void main(String[] args) {
ExampleService service = new ExampleService();
try {
service.performOperation("valid input");
service.performOperation(null); // This will throw our custom exception
} catch (CustomRuntimeException e) {
System.out.println("Caught CustomRuntimeException: " + e.getMessage());
// You could log this, handle it, or rethrow if necessary
}
}
}
Key Points:
- Extending RuntimeException: By extending RuntimeException, your exception becomes an unchecked exception, which means it doesn't need to be declared in a throws clause or caught.
- Custom Messages: You can provide custom error messages that are more meaningful in the context of your application.
- Flexibility: You can add more constructors or methods to your exception class if needed, like setting error codes, additional context, etc.
- Usage: Throwing custom exceptions allows you to encapsulate specific error conditions in your code, making error handling more targeted and meaningful.
This approach helps in creating more robust and self-documenting code, where the exceptions thrown are directly related to the business logic or specific error conditions in your application, rather than generic runtime exceptions.
However, it is possible for your program to throw an exception explicitly, using the throw statement. The general form of throw is shown
here:
throw ThrowableInstance;
Here, ThrowableInstance must be an object of type Throwable or a subclass of Throwable.
Primitive types, such as int or char, as well as non-Throwable
classes (such as String and Object), cannot be used as exceptions.
There are two ways you can obtain a Throwable object:
- using a parameter in a catch clause or
- creating one with the new operator.
The flow of execution stops immediately after the throw statement and any subsequent statements are not executed.
The nearest enclosing try block is inspected to see if it has a catch statement that matches the type of exception.
If it does find a match, control is transferred to that statement. If not, then the next enclosing try statement is inspected, and so on.
If no matching catch is found, then the default exception handler halts the program and prints the stack trace.
Here is a sample program that creates and throws an exception. The handler
that catches the exception rethrows it to the outer handler.