Exceptions
Like many other languages, Kotlin includes exceptions to indicate that something went wrong in your program. You have already seen exceptions that get thrown by the system or in the Kotlin standard library. You saw NumberFormatException when trying to convert certain strings into an Int, and you have seen NullPointerException (and the lack thereof) throughout this chapter so far.
But exceptions are not reserved for use by the system. You can use your own exceptions to indicate potential issues in your application.
Throwing an exception
Similar to many other languages, Kotlin allows you to manually signal that an exception has occurred.
You do this with the throw
keyword, and it is called throwing an exception. ...
Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.