From the course: Writing Secure Code for Android by Infosec

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Exception handling, part 2

Exception handling, part 2

- "Exception Handling Part 2." You can also have a throw expression returning nothing. This is a special type of value. Actually, it has no value. It's a special type. It indicates an unreachable block of code. You can use the nothing type in a function, and it will indicate that it will always throw an exception because it's unreachable. So here, we have some function, and it's going to return nothing, and it will always throw some kind of exception. Let's take a look at some Kotlin throw examples. We'll use the throw keyword, and we'll assign this thing called message to how do you feel. In this case, let's just say that our message length cannot be greater than 10 for whatever purpose. So if the message.length, and that means we're counting the characters here, is greater than 10, throw an illegal argument exception, "String is invalid," else, otherwise, just give us the length of the message. If we count all these characters, I can tell at a glance that it's more than 10. We means…

Contents