Throws
@Target([AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.CONSTRUCTOR]) annotation class Throws
typealias Throws = Throws
@Target([AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR]) annotation class Throws
For JVM
This annotation indicates what exceptions should be declared by a function when compiled to a JVM method.
Example:
@Throws(IOException::class)
fun readFile(name: String): String {...}
will be translated to
String readFile(String name) throws IOException {...}
For Common
This annotation indicates what exceptions should be declared by a function when compiled to a platform method in Kotlin/JVM and Kotlin/Native.
For Native
This annotation indicates what exceptions should be declared by a function when compiled to a platform method.
When compiling to Objective-C/Swift framework, non-suspend
functions having or inheriting this annotation are represented as NSError*
-producing methods in Objective-C and as throws
methods in Swift. Representations for suspend
functions always have NSError*
/Error
parameter in completion handler
When Kotlin function called from Swift/Objective-C code throws an exception which is an instance of one of the exceptionClasses or their subclasses, it is propagated as NSError
. Other Kotlin exceptions reaching Swift/Objective-C are considered unhandled and cause program termination.
Note: suspend
functions without @Throws
propagate only kotlin.coroutines.cancellation.CancellationException as NSError
. Non-suspend
functions without @Throws
don't propagate Kotlin exceptions at all.
Constructors
<init>
This annotation indicates what exceptions should be declared by a function when compiled to a platform method in Kotlin/JVM and Kotlin/Native.
<init>(vararg exceptionClasses: KClass<out Throwable>)
Properties
exceptionClasses
the list of checked exception classes that may be thrown by the function.
vararg val exceptionClasses: Array<out KClass<out Throwable>>
© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-throws/index.html