Throwing Exceptions

Created By: Geoffrey Challen
/ Updated: 2022-10-07

Next we'll continue exploring Java exceptions. First, we'll look at how we can throw our own exceptions. Next, we'll discuss the different types of Java exceptions and how they are used. Aren't errors fun? Let's do this.


Last time we looked at try-catch, Java's syntax for handling exceptions. But where do exceptions come from?

Some exceptions are generated directly by Java. For example, executing this code will generate a NullPointerException:

However, Java also provides a way for us to throw exceptions directly—the throw keyword! Let's see how that works.

Demonstrate how to use throw, probably in conjunction with try-catch.

When to throw

Java has several built-in exceptions that you may find useful in your own code. throw is particularly useful when there is no valid way to continue. Maybe your method has received bad input, or encounters a situation that it is unprepared to handle. We'll discuss this more in future lessons.

As a initial example, many of the situations that we've been handling using assert are better handled using the IllegalArgumentException. Let's look at an example:

Show how to use throw to handle bad inputs to methods or constructors.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Exception Types

In Java the Throwable class is the superclass of all objects that can be thrown, either using throw or in case of an error like a null pointer exception. Throwable has three important subclasses that are handled differently in Java: checked exceptions, unchecked exceptions, and errors.

Let's look at each category in turn. First, let's browse the documentation together!

Browse the Javadoc for Throwable, Exception, and RuntimeException. Read through some of the salient bits.

Checked Exceptions

Checked exceptions in Java must be handled. The compiler will check and enforce this! They are always generated using throw, which is how the compiler knows where they are and when you need to handle them.

The Exception class itself is checked, as are most of its descendants. Let's see a example of checked examples.

(Note that this example uses a playground that works a bit differently than the ones that we've used previously. I'll explain why in the walkthrough.)

Unchecked Exceptions

Unchecked exceptions don't need to be handled. But, the reason for this is that, in many cases, they arise from programmer error. Let's look at some examples.


The final category are Java errors. These are serious problems that you should not try to handle. According to the documentation:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

However, it is worth noting that assert generates an error. It is also possible to generate errors through programming errors, like this one:

This generates a StackOverflowError. You may get much more familiar with this kind of error soon...

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Show how to complete the homework problem above. Feel free to cover multiple approaches!

More Practice

Need more practice? Head over to the practice page.