From the course: Build Spring Boot Apps with the Kotlin Programming Language

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Creating custom exceptions

Creating custom exceptions

- [Instructor] That is really the basics of inheritance and extending classes. One of the things we talked about when we did the chapter on exceptions is that we might want to create our own exception classes. I'd like to use what we've learned about in terms of inheritance and extending to see how to do that. In Kotlin, to create a custom exception, what we need to do is to create a class that extends the class called Throwable. So let's do that. We've got our Basic Booking Manager. It requires an authorization key. I think what would be quite nice is that if the authorization key isn't valid, then when you try and instantiate Basic Booking Manager, you'll get an instance of our custom exception type and let's call it unauthorized user. We're going to create a new class called UnauthorizedUserException. I've used the British spelling of unauthorized here. If you'd rather use a zed, that's absolutely fine. What we're saying is that this needs to extend a class called Throwable. This…

Contents