Article directory
1. Handling Java exceptions in Kotlin
1. Exception handling in Java
If an exception is thrown in a Java function , if the function that throws the exception is called in Java , the exception must be handled , otherwise it will report
Unhandled exception: Xxx.XxException
error message;
Example of code that throws an exception: In the function of this exceptionDemo
code , IOException
an exception is thrown. When calling the exceptionDemo function in Java, the IOException
exception , otherwise an error will be reported during compilation;
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();
}
}
For the above code, an error is reported when compiling
Unhandled exception: java.io.IOException
Therefore, in Java code , the correct operation is to first use the try catch code block to catch the exception, and then handle the exception;
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();
}
}
}
The code compiles successfully , no errors are reported during compilation;
2. Call Java throwing exception function in Kotlin
Calling the above function in Kotlin code does not require mandatory handling of the thrown exception;
fun main() {
var javaMethod = JavaMethod()
javaMethod.exceptionDemo()
}
Although no exception is thrown during compilation, an exception will still be thrown during execution ;
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. Analyze Kotlin bytecode information
To view the bytecode data for this Kotlin , in the quick search, search for the "Show Kotlin Bytecode" option,
On the "Kotlin Bytecode" page , click the "Decompile" button at the top to decompile bytecode data into Java code;
In the decompiled Java code, there are compile-time errors;
4. Catching exceptions in Kotlin
In Kotlin, try...catch code blocks can be used to capture Java exceptions;
Code example:
import java.io.IOException
fun main() {
var javaMethod = JavaMethod()
try {
javaMethod.exceptionDemo()
} catch (e: IOException) {
println("Catch IOException")
}
}
Results of the :
Catch IOException
2. Handling Kotlin exceptions in Java
1. Exception handling in Kotlin method
In the Kotlin function, an exception is thrown;
If you call a Kotlin function that throws an exception in Kotlin, you can directly use the try catch code block to catch and handle the exception;
Code example:kotlinException
The function in Kotlin code throws IOException
an exception message,
import java.io.IOException
class KotlinMethod {
fun kotlinException(){
throw IOException()
}
}
fun main() {
try {
KotlinMethod().kotlinException()
} catch (e: IOException) {
println("Catch IOException")
}
}
Results of the :
Catch IOException
2. Call Kotlin exception method in Java
In Java, if you directly call the function that throws an exception in Kotlin, an error will be reported at runtime;
public class JavaMethod {
public static void main(String[] args) {
new KotlinMethod().kotlinException();
}
}
If you use the try...catch... code block to forcibly catch the exception thrown in the Kotlin function , a compile-time error will occur;
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");
}
}
}
Error message when compiling:
Exception 'java.io.IOException' is never thrown in the corresponding try block
3. Use the @Throws annotation to indicate exceptions
If you want to handle exceptions thrown by Kotlin in Java, then the function that throws exceptions in Kotlin must use the @Throws annotation to annotate the exception;
In the @Throws annotation, just pass in the Class bytecode class;
@Throws(IOException::class)
The Kotlin code is as follows:
import java.io.IOException
class KotlinMethod {
@Throws(IOException::class)
fun kotlinException(){
throw IOException()
}
}
fun main() {
try {
KotlinMethod().kotlinException()
} catch (e: IOException) {
println("Catch IOException")
}
}
The Java code is as follows:
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");
}
}
}
The result of the operation is as follows:
3. Function type interoperability between Kotlin and Java
1. Java calls the anonymous function solution in Kotlin
Ordinary functions, function type variables, and anonymous functions in Kotlin can all call each other, which is a grammatical feature of Kotlin;
But in Java, Lambda expressions are only supported after JDK1.8, so anonymous functions cannot be called directly in the Java language;
In order to solve the above problems, Kotlin provides the FunctionN interface to deal with the above problems, and the value range of N is 0 ~ 22;
- Function0 means that the function has 0 parameters;
- Function1 means that the function has 1 parameter;
- Function22 means that the function has 22 parameters;
Each FunctionN interface provides an invoke function ,
Call the invoke function,
Pass parameters in the function,
The corresponding anonymous function can be executed;
2. Anonymous function code example in Kotlin
In Kotlin, a function type variable is defined . In the following KotlinMethod
class , a lambda type member is defined, which is a function type variable.
- The function type of the function type variable is (String)->Unit;
- The value of the function type variable is an anonymous function/Lambda expression/closure;
class KotlinMethod {
val lambda = {
name: String ->
println("name : $name")
}
}
fun main() {
KotlinMethod().lambda("Tom")
}
In Kotlin, you can directly use a function type variable as a function , KotlinMethod().lambda("Tom")
use it as the function name, and pass in parameters to call the function;
3. Code example of calling Kotlin anonymous function in Java
In Java, from the obtained variable of kotlin.jvm.functions.Function1
type , call its invoke
function to execute the function corresponding to the function type variable of Kotlin;
public class JavaMethod {
public static void main(String[] args) {
KotlinMethod kotlinMethod = new KotlinMethod();
kotlinMethod.getLambda().invoke("Jerry");
}
}
kotlin.jvm.functions.Function1
The prototype of the type is as follows −
public interface Function1<in P1, out R> : Function<R> {
/** Invokes the function with the specified argument. */
public operator fun invoke(p1: P1): R
}
The execution result is as follows: