以下是我在Doug Hellman网站上发现的一个名为"masking_exceptions_catch.py"的文件中的示例.我暂时无法找到该链接.抛出throws()中引发的异常,同时报告由cleanup()引发的异常.
在他的文章中,Doug评论说处理是非直观的.中途期望它是Python版本中的一个bug或限制(大约在2009年),我在Mac的当前生产版本中运行它(2.7.6).它仍然报告cleanup()的异常.我发现这有点惊人,并希望看到它是如何实际正确或理想的行为的描述.
#!/usr/bin/env python
import sys
import traceback
def throws():
raise RuntimeError('error from throws')
def nested():
try:
throws()
except:
try:
cleanup()
except:
pass # ignore errors in cleanup
raise # we want to re-raise the original error
def cleanup():
raise RuntimeError('error from cleanup')
def main():
try:
nested()
return 0
except Exception, err:
traceback.print_exc()
return 1
if __name__ == '__main__':
sys.exit(main())
Run Code Online (Sandbox Code Playgroud)
节目输出:
$ python masking_exceptions_catch.py
Traceback (most recent call last):
File "masking_exceptions_catch.py", line 24, in main
nested()
File "masking_exceptions_catch.py", line …Run Code Online (Sandbox Code Playgroud) 我看了一下,但我找不到关于直接从golang源代码构建的Android版本的讨论,很少或没有Java.
我看到有一个Android 9(Go版)版本.但看起来它只是更多Java与Golang绑定:https://www.android.com/versions/go-edition .或者这是原生GoAndroid吗?
我有一个抽象超类,带有一个返回子类实例的工厂。是否有可能有一个仅在超类中实现的方法?例如,在下面的代码中,是否可以删除 Wind::act()?
abstract class Element {
final String action; // what it does
String act() => action; // do it
factory Element() {
return new Wind();
}
}
class Wind implements Element {
final action = "blows";
act() => action; // Why is this necessary?
}
void main() {
print(new Element().act());
}
Run Code Online (Sandbox Code Playgroud)
删除 Wind::act() 时,出现缺少它的错误。此外,当扩展而不是实现超类时,省略子类实现不会导致错误。但对于工厂方法,扩展不是一种选择。