Java Exception Handling Generating Exceptions

Learning objective: By the end of this lesson, students will be able to generate Java exceptions in their code using the throw keyword.

Why would you want to throw an exception?

We know that an exception is an unexpected and unintentional error event that can happen during the execution of a program and disrupt its normal flow. While building Java applications, there might be situations where we might have to intentionally stop the user from proceeding further because something has gone wrong.

One of the choices with you as a programmer is to reuse Java’s way of stopping further code execution using exceptions. To generate our own exceptions, we need to understand how Java Runtime Engine (JRE) throws an exception and mimic it.

How Java throws an exception

Unintended events in Java can arise from different situations, such as wrong data entered by the user, hardware failure, network connection failure, or a database server being down. Java creates an exception object when an error occurs while executing a statement.

The exception object contains a lot of debugging information, such as method hierarchy (call stack), the line number where the exception occurred, and the type of exception. Here’s the sequence of how an exception is generated in Java:

The process of creating an Exception object from one of the Exception classes and handing it over to the JRE is called throwing an exception.

Programmatic exception throwing

Exceptions can be created using the throw statement in Java.

Syntax:

throw new ExceptionType("Error message");

Let’s look at an example code to enforce a business rule that customers cannot withdraw more than their account balance amount.

public void processWithdrawl(double amount, double balance) {
    if (amount > balance) {
        throw new IllegalArgumentException("Insufficient funds for transaction.");
    }
}

ExceptionType research

8 min

Research and find various built-in exceptions that can be used for ExceptionType in the throw statement.