我工作的一个Java项目,这个项目我做了我第一次尝试科特林内。我开始把一些类科特林同在的IntelliJ IDEA提供的JavaToKoltin转换器。在别人我的自定义异常,现在转换成科特林。但有了这个异常处理不起作用正确了。
如果我把我的自定义异常(例如一个MyCustomKotlinException.kt
在Java代码中),这个异常没有钓到(见下面的代码)。
// Example.java
package foo
import java.util.*;
import java.lang.*;
import java.io.*;
import foo.MyCustomKotlinException;
class Example
{
public static void main (String[] args)
{
try {
// Do some stuff
// if Error
MyCustomKotlinException e = new MyCustomKotlinException("Error Message");
throw e;
} catch (MyCustomKotlinException e) { // <-- THIS PART IS NEVER REACHED
// Handle Exception
} catch (Throwable e) {
e.printStackTrace(); <-- This is catched
} finally {
// Finally ...
}
}
}
因此,谁能向我解释为什么例外是抓不住。MyCustomKotlinException
从Kotlins继承RuntimeException
,这只是一个别名java.lang.RuntimeException
。
// MyCustomKotlinException.kt
package foo
class MyCustomKotlinException(err: String) : RuntimeException(err)
更新:
我扔的部分分成2线(例如创建和投掷),发现问题不是投掷。try块是实例创建后离开。是什么毛病我的实例创建这个科特林类的?
UPDATE2:
我添加了第二个catch块与Throwable
和下面的Throwable被捕获。
java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
...
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
UPDATE3:
改变了标题,以正确的错误和固定的问题,将所有项目文件的jar(见下面的答案)。添加科特林运行时的lib到gradle这个不适合我的工作。
添加所有项目文件的罐子修复了这个问题对我来说。添加以下行到我的build.gradle
jar {
manifest {
attributes ...
}
// This line of code recursively collects and copies all of a project's files
// and adds them to the JAR itself. One can extend this task, to skip certain
// files or particular types at will
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
更新:改变configurations.compile.collect
以configurations.compileClasspath.collect
根据本下面的答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句