Book Image

Programming Kotlin

Book Image

Programming Kotlin

Overview of this book

Quickly learn the fundamentals of the Kotlin language and see it in action on the web. Easy to follow and covering the full set of programming features, this book will get you fluent in Kotlin for Android.
Table of Contents (20 chapters)
Programming Kotlin
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Exception handling


Handling of exceptions is almost identical to the way Java handles exceptions with one key difference in Kotlin all exceptions are unchecked.

As a reminder, checked exceptions are those that must be declared as part of the method signature or handled inside the method. A typical example would be IOException, which is thrown by many File functions, and so ends up being declared in many places throughout the IO libraries.

Unchecked exceptions are those that do not need to be added to method signatures. A common example would be the all too familiar NullPointerException, which can be thrown anywhere. If this was a checked exception, literally every function would need to declare it!

In Kotlin, since all exceptions are unchecked, they never form part of function signatures.

The handling of an exception is identical to Java, with the use of try, catch, and finally blocks. Code that you wish to handle safely can be wrapped in a try block. Zero or more catch blocks can be added to...