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.

Throwing exceptions

Throwing exceptions

- [Instructor] Okay, so let's move on. And I'd like to write a Kotlin function that can throw an exception. So let's create a function that calls Thread.sleep. So I'm going to remove this out of here. In fact, actually I'm going to clear out my main method for now. And create a second function up here which I think we'll call divide. And the idea is we can pass in two numbers to this function and it's going to return a double. And in here what we're going to do is we're going to return a divided by b. I'll need to cast a as a double first, I think, to get a sense of an answer out. But before that happens, we'll sleep for one second with Thread.sleep. Now in Java then, as we've already seen, if we wrote this function we'd either need to surround Thread.sleep in a try-catch block or we'd need to add the throws declaration to this function definition. If I write the same thing up here, just so we can see it, I'll need to make this a static function in the main class here so it's…

Contents