Next we’ll continue exploring Kotlin exceptions.
First, we’ll look at how we can
throw our own exceptions.
Next, we’ll discuss the different types of Kotlin exceptions and how they are used.
Aren’t errors fun?
Let’s do this.
Last time we looked at
try-catch, Kotlin’s syntax for handling exceptions.
But where do exceptions come from?
Some exceptions are generated directly by Kotlin.
For example, executing this code will generate a
However, Kotlin also provides a way for us to
throw exceptions directly—the
Let’s see how that works.
Kotlin 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.
We’ve already seen one convenient way to throw exceptions in Kotlin:
require throws an
IllegalArgumentException, but provides syntax that is
somewhat more convenient that
Let’s look at an example:
exceptionable accepting a single
Int as a parameter.
require that the int is between 0 and 3, inclusive.
Int is 0, you should return an
If it's 1, you should return a
If it's 2, you should return a
And if it's 3, you should return a
In Kotlin 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 two subclasses that are handled differently.
We’ve been discussing exceptions so far.
The second category is 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
You may get much more familiar with this kind of error soon…
For those of you familiar with Java, you may remember that the Java compiler forces you to handle certain types of exceptions, known as checked exceptions.
Kotlin differs from Java in this regard. Kotlin does not have checked exceptions. It does not force you to handle any exceptions. There are reasons for this design choice, which you can start reading more about here if you are interested.
Create a class
CapacityManager to manage restaurant dining capacity.
You should provide a primary constructor that allows the capacity of the restaurant to be set as an
Int when the
CapacityManager is created.
You don't need to provide either a getter or a setter for the capacity, which will not change.
Provide two methods:
Each accepts a count of guests (as an
Int) that are either arriving (
enter) or departing (
leave) from the
Both should return the number of guests after the operation completes as an
If the count is negative or zero, both methods should throw an
enter should also throw a
IllegalStateException if the new party would exceed the capacity of the restaurant.
This restaurant also refuses to admit parties larger than 8, and if one arrives
enter should throw a
PartyTooLarge exception, which you can create without importing:
leave should also throw an
IllegalStateException if a party leaves that is larger than 8, or if the leave
would cause the number of diners to fall below zero, since both represent mistakes in our
leave, if an exception is thrown, the number of diners should not be changed.
When you are finished, here is how your class should work:
Do not use
assert to solve this problem!
However, you can still use
check to generate some (but not all) of the exceptions mentioned above.
Need more practice? Head over to the practice page.