gpt4 book ai didi

kotlin - 如何在 Kotlin 中同时捕获多个异常?

转载 作者:IT老高 更新时间:2023-10-28 13:26:24 59 4
gpt4 key购买 nike

try { 

} catch (ex: MyException1, MyException2 ) {
logger.warn("", ex)
}

try { 

} catch (ex: MyException1 | MyException2 ) {
logger.warn("", ex)
}

结果出现编译错误:Unresolved reference: MyException2.

如何在 Kotlin 上同时捕获多个异常?

最佳答案

更新:为以下问题投票 KT-7128如果你想让这个功能登陆 Kotlin。 感谢@Cristan

据此thread目前不支持此功能。

abreslav - JetBrains Team

Not at the moment, but it is on the table

不过,您可以模仿多重捕获:

try {
// do some work
} catch (ex: Exception) {
when(ex) {
is IllegalAccessException, is IndexOutOfBoundsException -> {
// handle those above
}
else -> throw ex
}
}

关于kotlin - 如何在 Kotlin 中同时捕获多个异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36760489/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com