Kotlin

Exceptions

Swift

Exception Classes

                  throw Exception("Hi There!")
                
                    throw Exception("Hi There!")
                  
                  try {
    // some code
}
catch (e: SomeException) {
    // handler
}
finally {
    // optional finally block
}
                
                    try {
    // some code
}
catch (e: SomeException) {
    // handler
}
finally {
    // optional finally block
}
                  

Try is an expression

                  val a: Int? = try { parseInt(input) } catch (e: NumberFormatException) { null }
                
                    let a: Int? = try { parseInt(input) } catch (e: NumberFormatException) { null }
                  

Checked Exceptions

                  Appendable append(CharSequence csq) throws IOException;
                
                    Appendable append(CharSequence csq) throws IOException;
                  
                  try {
    log.append(message)
}
catch (IOException e) {
    // Must be safe
}
                
                    try {
    log.append(message)
}
catch (IOException e) {
    // Must be safe
}
                  

The Nothing type

                  val s = person.name ?: throw IllegalArgumentException("Name required")
                
                    let s = person.name ?? throw IllegalArgumentException("Name required")
                  
                  fun fail(message: String): Nothing {
    throw IllegalArgumentException(message)
}
                
                    func fail(message: String): Nothing {
    throw IllegalArgumentException(message)
}
                  
                  val s = person.name ?: fail("Name required")
println(s)     // 's' is known to be initialized at this point
                
                    let s = person.name ?? fail("Name required")
print(s)     // 's' is known to be initialized at this point
                  
                  val x = null           // 'x' has type `Nothing?`
val l = listOf(null)   // 'l' has type `List<Nothing?>
                
                    let x = null           // 'x' has type `Nothing?`
let l = listOf(null)   // 'l' has type `List<Nothing?>