小编Ali*_*Ali的帖子

使用 @Throws 时强制 android studio 编译器使用 try-catch - Kotlin

在 中Java,Android Studio 有一个功能可以避免调用throws没有try/catch块的异常方法,并且它的自动完成建议:Add exception to method signatureSurround with try/catch

Kotlin我们没有throws关键字,应该使用@Throws()它。问题是,它Android Studio不再迫使我们包围try/catch。另一方面,它并不强迫我们使用 类似try/catchjava methods进步。throws ExceptionIO

测试.java

public class Test {

    public void doSomeWork1(int a) throws Exception {
        if (a == -1) {
            throw new Exception("some error");
        }
    }

    public void main() {
        doSomeWork1(-1);
    }
}
Run Code Online (Sandbox Code Playgroud)

测试.kt

class Test {

    @Throws(Exception::class)
    fun doSomeWork1(a: Int) { …
Run Code Online (Sandbox Code Playgroud)

android try-catch throws kotlin android-studio

5
推荐指数
0
解决办法
1272
查看次数

标签 统计

android ×1

android-studio ×1

kotlin ×1

throws ×1

try-catch ×1