Exceptions
Exceptions
Exception Classes
All exception classes in Kotlin are descendants of the class Throwable
. Every exception has a message, stack trace and an optional cause.
To throw an exception object, use the throw-expression
throw MyException("Hi There!")
To catch an exception, use the try-expression
try { // some code } catch (e: SomeException) { // handler } finally { // optional finally block }
There may be zero or more catch blocks. finally blocks may be omitted. However at least one catch or finally block should be present.
Try is an expression
try is an expression, i.e. it may have a return value.