getCauseAs

inline fun <X : Throwable> Throwable.getCauseAs(): X?(source)

Returns this's cause, cast to X.

Prefer this method instead of manually casting an exception's cause. For example, (e as IOException).getCause() throws a ClassCastException that discards the original exception e if the cause is not an IOException, but e.getCauseAs<IOException>() keeps e as the ClassCastException's cause.

See also

Throws

if the cause cannot be cast to the expected type. The ClassCastException's cause is this.