Java - 如何做Python的尝试,除了其他
除了Java中的其他方法,就像我在Python中一样,我该如何尝试?
例:
try:
something()
except SomethingException,err:
print 'error'
else:
print 'succeeded'
我看到尝试和捕捉提到,但没有别的。
除了Java中的其他方法,就像我在Python中一样,我该如何尝试?
例:
try:
something()
except SomethingException,err:
print 'error'
else:
print 'succeeded'
我看到尝试和捕捉提到,但没有别的。
我不完全相信我喜欢它,但这相当于Python的其他产品。它消除了将成功代码放在 try 块末尾所识别的问题。
bool success = true;
try {
something();
} catch (Exception e) {
success = false;
// other exception handling
}
if (success) {
// equivalent of Python else goes here
}
这又如何呢?
try {
something();
} catch (Exception e) {
// exception handling
return;
}
// equivalent of Python else goes here
当然,在某些情况下,您希望在try/catch/else之后放置更多代码,并且此解决方案不适合那里,但是如果它是方法中唯一的尝试/捕获块,则可以使用。