gpt4 book ai didi

Java - 如何做 Python 的尝试,除了其他

转载 作者:IT老高 更新时间:2023-10-28 20:40:25 25 4
gpt4 key购买 nike

除了 Python 中的 else 之外,我如何在 Java 中进行尝试?

例子:

try:
something()
except SomethingException,err:
print 'error'
else:
print 'succeeded'

我看到提到了 try and catch,但没有别的。

最佳答案

我并不完全相信我喜欢它,但这相当于 Python 的 else。通过将成功代码放在 try block 的末尾,它消除了已识别的问题。

bool success = true;
try {
something();
} catch (Exception e) {
success = false;
// other exception handling
}
if (success) {
// equivalent of Python else goes here
}

关于Java - 如何做 Python 的尝试,除了其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3478222/

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