Exceptions
All exception classes in Kotlin inherit the Throwable class.
Every exception has a message, a stack trace, and an optional cause.
To throw an exception object, use the throw expression:
To catch an exception, use the try... catch expression:
There may be zero or more catch blocks, and the finally block may be omitted.
However, at least one catch or finally block is required.
try is expression
try is an expression, which means it can have a return value:
The returned value of a try expression is either the last expression in the try block or the last expression in the catch block (or blocks).
The contents of the finally block don't affect the result of the expression.
Checked exceptions
Kotlin does not have checked exceptions, unlike Java.
The Nothing type
throw is an expression in Kotlin, so you can use it, for example, as part of an Elvis expression:
The throw expression has the type Nothing.
This type has no values and is used to mark code locations that can never be reached.
In your own code, you can use Nothing to mark a function that never returns:
When you call this function, the compiler will know that the execution doesn't continue beyond the call:
Since fail has a return type of Nothing, the compiler knows that the program cannot continue beyond the call to fail. Therefore, the println(s) statement will not be executed if person?.name is null.
Code comment means that, When println(s) is executed, it's known that s has been initialized and no exception was thrown.