Tag: 异常处理

在服务编排场景中,如何处理没有检查异常的错误?

大多数编程语言没有检查异常(如C#和Kotlin)。 所以,我试图找到一个更好的方法来处理我的项目,而不使用检查的异常。 该项目使用Java,我们控制远程EJB的可能的验证错误。 像这样的东西: lookupSomeRemoteEjb().createCustomer(/** lots of informations */); 这个方法抛出了许多的验证异常,例如: DocumentoAlreadyExistsException(); InvalidNameException(); InvalidBirthDateException(); 等等。 这种例外可能发生在不同级别的不同课程中。 像这样的例子: CustomerRemoteEjb.class // tell to the another system what error happened CustomerService.class //can throw some errors about customer PersonService.class // can throw some errors about person DocumentService.class // can throw some errors about document AddressService.class // can throw some errors […]

有什么简单的方法来看看Kotlin函数抛出什么异常?

我主要了解检查异常的潜在问题,以及Kotlin忽略它们的原因。 然而,我遇到的问题是我找不到任何清楚地向调用者指出函数可能抛出什么异常的万无一失的方式。 我已经在Python中遇到了无数次的问题,我的程序在运行好几个月后会崩溃,因为我没有意识到我正在使用某个库的函数会引发特定的异常。 尽管被迫捕捉异常可能是相当有问题的,但是清楚地看到函数可能抛出的所有可能异常是很好的。 所以回到这个问题,有什么简单的方法来看看函数在Kotlin中抛出什么异常? 那么从Kotlin调用Java编写的方法呢? 即使只是在工具(intelliJ)。 我不想把它写在javadoc或kdoc中,因为你正在使用的函数的编写者可能会忽略它。

为什么Kotlin收到这样的UndeclaredThrowableException而不是ParseException?

我有一个extension方法,将字符串转换为Kotlin中的日期。 fun String.convertToDate() : Date { var pattern: String = "dd-mm-yyyy" val dateFormatter = SimpleDateFormat(pattern) return dateFormatter.parse(this) // parse method throw ParseException } 这是我试图捕捉可能的异常的代码。 try { "22—2017".convertToDate() } catch (ex: ParseException) { // ParseException supposed to be caught in this block logger.error("Parse exception occur") } catch (ex: Exception) { // ParseException caught in this block […]

Gradle项目:java.lang.NoClassDefFoundError:kotlin / jvm / internal / Intrinsics

我正在开发一个Java项目,在这个项目中,我第一次尝试了Kotlin。 我开始使用Intellij Idea中提供的JavaToKoltin转换器将一些类转换为Kotlin。 其中我的自定义例外现在转换为Kotlin。 但是,这个异常处理不再正确工作了。 如果我在java代码中抛出一个自定义异常(例如MyCustomKotlinException.kt ),那么这个异常不会被捕获(见下面的代码)。 // 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 […]