Effective Kotlin: Exceptions

Let’s see how we can apply Joshua Bloch’s Effective Java in the Kotlin world. Today’s topic is Exceptions.

Item 69: Use exceptions only for exceptional conditions

There is nothing special in Kotlin about this item.

Item 70: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors

There is no checked exception in Kotlin, but we can use the @Throws annotation to support Java interoperability, and the same idea should still be followed.

Item 71: Avoid unnecessary use of checked exceptions

Same as Item 70.

Item 72: Favor the use of standard exceptions

There is nothing special in Kotlin about this item.

Item 73: Throw exceptions appropriate to the abstraction

There is nothing special in Kotlin about this item.

Item 74: Document all exceptions thrown by each method

In Kotlin, we can use the @throws tag to document exceptions, and the @Throws annotation to support Java interoperability.

Item 75: Include failure-capture information in detail messages

There is nothing special in Kotlin about this item.

Item 76: Strive for failure atomicity

There is nothing special in Kotlin about this item.

Item 77: Don’t ignore exceptions

In Kotlin, because there is no checked exception, we can easily letting exceptions propagating if we don’t want to do anything.


See also

comments powered by Disqus