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.

Exception handling

Exception handling

- [Instructor] Let's have a look now at exceptions, and what I think I'd like to do to try and discourage my Java developers from trying to add customers, is let's create a method in our Customer Database called Add Customer. Which all it's going to do is throw an Illegal Access Exception. I've chosen that particular Exception type because it's a Checked Exception. So, I want to demonstrate how this works. So, we'll create a function here, which can be called Add Customer, which will take a Customer object. So, let's call it C. And then, all we're going to do in here is throw a new Illegal Access Exception. And I think I'll put the text in here, You Cannot Add a Customer. Okay so, now let's see what happens if we try and rather than calling Customers.Add, our new customer called Phil, let's say, DB.AddCustomer our new customer called Phil. So, of course, that's going to compile. Let's run it and see that we'll get that Illegal Access Exception. Okay, that's exactly what we expected…

Contents