【Kotlin】Kotlin 与 Java 互操作 ③ ( Kotlin 中处理 Java 异常 | Java 中处理 Kotlin 异常 | @Throws 注解处理异常 | 函数类型互相操作 )





一、Kotlin 中处理 Java 异常




1、Java 中异常操作


Java 函数中 抛出 异常 , 如果 在 Java 中调用该 抛出异常 的函数 , 则 必须处理该异常 , 否则编译时就会报

Unhandled exception: Xxx.XxException

错误信息 ;


抛出异常代码示例 : 在该代码的 exceptionDemo 函数中 , 抛出了 IOException 异常 , 在 Java 中调用 exceptionDemo 函数时 , 必须处理该函数抛出的 IOException 异常 , 否则在编译时会报错 ;

import java.io.IOException;

public class JavaMethod {
    
    

    public void exceptionDemo() throws IOException {
    
    
        throw new IOException();
    }

    public static void main(String[] args) {
    
    
        JavaMethod javaMethod = new JavaMethod();
        javaMethod.exceptionDemo();
    }
}

上述代码 , 在 编译时报错

Unhandled exception: java.io.IOException

在这里插入图片描述

因此 , 在 Java 代码中 , 正确的操作是 , 先使用 try catch 代码块捕获该异常 , 然后处理异常 ;

import java.io.IOException;

public class JavaMethod {
    
    

    public void exceptionDemo() throws IOException {
    
    
        throw new IOException();
    }

    public static void main(String[] args) {
    
    
        JavaMethod javaMethod = new JavaMethod();
        try {
    
    
            javaMethod.exceptionDemo();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

该代码编译通过 , 没有在编译时报错 ;

在这里插入图片描述


2、Kotlin 中调用 Java 抛出异常函数


在 Kotlin 代码中调用上述 函数 , 可以不需要 强制处理 抛出的异常 ;

fun main() {
    
    
    var javaMethod = JavaMethod()
    javaMethod.exceptionDemo()
}

在这里插入图片描述

虽然在编译时没有抛出异常 , 但是 执行时 , 还是会抛出异常 ;

Exception in thread "main" java.io.IOException
	at JavaMethod.exceptionDemo(JavaMethod.java:6)
	at HelloKt.main(Hello.kt:3)
	at HelloKt.main(Hello.kt)

在这里插入图片描述


3、分析 Kotlin 字节码信息


查看 该 Kotlin 的字节码数据 , 在 快速搜索 中 , 搜索 " Show Kotlin Bytecode " 选项 ,

在这里插入图片描述

在 " Kotlin Bytecode " 页面 , 点击顶部的 " Decompile " 按钮 , 将字节码数据反编译成 Java 代码 ;

在这里插入图片描述

在反编译后的 Java 代码中 , 存在 编译时 错误 ;

在这里插入图片描述


4、Kotlin 中捕获异常


在 Kotlin 中可以 使用 try…catch 代码块 , 捕获 Java 异常 ;

代码示例 :

import java.io.IOException

fun main() {
    
    
    var javaMethod = JavaMethod()
    try {
    
    
        javaMethod.exceptionDemo()
    } catch (e: IOException) {
    
    
        println("Catch IOException")
    }
}

执行结果 :

Catch IOException

在这里插入图片描述





二、Java 中处理 Kotlin 异常




1、Kotlin 方法中抛出异常处理


在 Kotlin 的 函数 中 , 抛出异常 ;

如果 在 Kotlin 中 调用 抛出异常 的 Kotlin 函数 , 直接使用 try catch 代码块捕获并处理该异常即可 ;


代码示例 : 在 Kotlin 代码中的 kotlinException 函数抛出了 IOException 异常信息 ,

import java.io.IOException

class KotlinMethod {
    
    
    fun kotlinException(){
    
    
        throw IOException()
    }
}

fun main() {
    
    
    try {
    
    
        KotlinMethod().kotlinException()
    } catch (e: IOException) {
    
    
        println("Catch IOException")
    }
}

执行结果 :

Catch IOException

在这里插入图片描述


2、Java 中调用 Kotlin 异常方法


在 Java 中 , 直接调用 Kotlin 抛出异常的函数 , 运行时报错 ;

public class JavaMethod {
    
    
    public static void main(String[] args) {
    
    
        new KotlinMethod().kotlinException();
    }
}

在这里插入图片描述

如果 使用 try…catch… 代码块 强行捕获 Kotlin 函数中抛出的异常 , 会出现编译时错误 ;

import java.io.IOException;

public class JavaMethod {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            new KotlinMethod().kotlinException();
        } catch (IOException e) {
    
    
            System.out.println("Catch IOException");
        }
    }
}

编译时报错信息 :

Exception 'java.io.IOException' is never thrown in the corresponding try block

在这里插入图片描述


3、使用 @Throws 注解注明异常


如果 要在 Java 中处理 Kotlin 抛出的异常 , 那么 在 Kotlin 抛出异常的函数 , 必须使用 @Throws 注解注明异常 ;

在 @Throws 注解中 , 传入 Class 字节码类即可 ;

@Throws(IOException::class)

Kotlin 代码如下 :

import java.io.IOException

class KotlinMethod {
    
    
    @Throws(IOException::class)
    fun kotlinException(){
    
    
        throw IOException()
    }
}

fun main() {
    
    
    try {
    
    
        KotlinMethod().kotlinException()
    } catch (e: IOException) {
    
    
        println("Catch IOException")
    }
}

Java 代码如下 :

import java.io.IOException;

public class JavaMethod {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            new KotlinMethod().kotlinException();
        } catch (IOException e) {
    
    
            System.out.println("Catch IOException");
        }
    }
}

运行结果如下 :

在这里插入图片描述





三、Kotlin 与 Java 之间的函数类型互操作




1、Java 调用 Kotlin 中的 匿名函数解决方案


Kotlin 中 普通函数 , 函数类型变量 , 匿名函数 都可以互相调用 , 这是 Kotlin 的语法特性 ;

但是 在 Java 中 , 只有从 JDK1.8 之后才支持 Lambda 表达式 , 因此在 Java 语言中不能直接调用 匿名函数 ;


Kotlin 为了解决上述问题 , 提供了 FunctionN 接口 处理上述问题 , N 的取值范围是 0 ~ 22 ;

  • Function0 表示 函数有 0 个参数 ;
  • Function1 表示 函数有 1 个参数 ;
  • Function22 表示 函数有 22 个参数 ;

每个 FunctionN 接口 都提供了一个 invoke 函数 ,

调用 该 invoke 函数 ,

在函数中传入参数 ,

即可执行对应的 匿名函数 ;


2、Kotlin 中 匿名函数代码示例


在 Kotlin 中 , 定义了 函数类型变量 , 下面的 KotlinMethod 类中 , 定义了 lambda 类型成员 , 该成员是 函数类型变量 ,

  • 该 函数类型变量的 函数的类型是 (String)->Unit ;
  • 该 函数类型变量的 值 是一个 匿名函数 / Lambda 表达式 / 闭包 ;
class KotlinMethod {
    
    
    val lambda = {
    
    
        name: String ->
        println("name : $name")
    }
}

fun main() {
    
    
    KotlinMethod().lambda("Tom")
}

在 Kotlin 中 , 可以 直接将 函数类型变量 当做函数使用 , KotlinMethod().lambda("Tom") 将其作为 函数名 , 传入参数 , 即可调用该函数 ;


3、Java 中 调用 Kotlin 匿名函数代码示例


在 Java 中 , 从 获取的是 kotlin.jvm.functions.Function1 类型的变量 , 调用其 invoke 函数 , 即可 执行该 Kotlin 的函数类型变量对应的函数 ;

public class JavaMethod {
    
    
    public static void main(String[] args) {
    
    
        KotlinMethod kotlinMethod = new KotlinMethod();
        kotlinMethod.getLambda().invoke("Jerry");
    }
}

kotlin.jvm.functions.Function1 类型的原型如下 :

public interface Function1<in P1, out R> : Function<R> {
    
    
    /** Invokes the function with the specified argument. */
    public operator fun invoke(p1: P1): R
}

执行结果如下 :
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/129138286