Exceptions in kotlin

Keywords: Java

Article Directory
Preface
Introduction to kotlin anomaly
Use of kotlin exception
Summary

Preface

Exceptions in Java can be caught by try/catch only. So what is the difference between kotlin and java? Is it simpler? What are the new advantages? Let's explain below?

Introduction to kotlin exception

You can return a value in catch
//Below are kotlin-specific
You can return null in catch
You can use return in catch to jump out of the method
Separate between anomalies under examination and anomalies under examination
try/catch can be used as an expression

Use of Exceptions

1. In general, return values in catch (note the difference between java and kotlin)

//java
public class TryCatchAndFinally {
    public static void main(String[] args){
        BufferedReader bufferedReader = new BufferedReader(new StringReader("234"));
        try {
            System.out.println(readNumber(bufferedReader));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static int readNumber(BufferedReader reader) throws IOException {
        try {
            String readLine = reader.readLine();
            return Integer.parseInt(readLine);
        } catch (NumberFormatException e) {
            return -1; //Not a number, return -1
        }finally {
            reader.close();
            return -1;
        }

    }
}
//kotlin
fun readNumber(reader: BufferedReader): Int? {//Return value can be null
    try {
        return Integer.parseInt(reader.readLine())
    }catch (e: NumberFormatException){
        return null;
    }finally {
        reader.close()
    }
}

fun main(args: Array<String>) {
    val bufferedReader = BufferedReader(StringReader("234"))
    println(bufferedReader)
}

In the code above, it is also important to note that kotlin does not distinguish between checked and unchecked exceptions.
Normally, if an exception is checked, write an exception thrown, such as
IOException s are checked exceptions and must throws in java, but kotlin can be written without errors in the compiler.

2. Return null or direct return in catch

//kotlin
fun readNumber(reader: BufferedReader): Int? {//Return value can be null
    try {
        return Integer.parseInt(reader.readLine())
    }catch (e: NumberFormatException){
        return null;
       //Return //You can use return to jump out of the method directly
    }finally {
        reader.close()
    }
}

fun main(args: Array<String>) {
    val bufferedReader = BufferedReader(StringReader("234"))
    println(bufferedReader)
}

3. try/catch can be used as an expression

import java.io.BufferedReader
import java.io.StringReader

fun readNumber(reader: BufferedReader){
    val number = try {
        Integer.parseInt(reader.readLine())
    }catch (e: NumberFormatException){
        null
    }
    println(number)
}

fun main(args: Array<String>) {
    val bufferedReader = BufferedReader(StringReader("not a number"))
    readNumber(bufferedReader)
}

The code above shows that if the try block executes correctly, the last expression is the result, and if an exception is caught, the last expression in the catch is the result.

summary

Abnormal writing in kotlin is simpler
Abnormal writing in kotln is more diverse

It's a pleasure to be able to help you a little.:)
Welcome to Long Press ->Identify the QR code in the picture or sweep the public number that pays attention to me:

My csdn: http://blog.csdn.net/shenshizhong
My brief book: http://www.jianshu.com/u/345daf0211ad

Posted by mathewvp on Wed, 22 May 2019 12:41:26 -0700