问题描述
我知道使用谷歌搜索我可以找到合适的答案,但我更喜欢听听您的个人(也许是技术)意见.
Java 和 C# 抛出异常的差异的主要原因是什么?
在 Java 中,抛出异常的方法的签名必须使用throws"关键字,而在 C# 中,您在编译时不知道是否可以抛出异常.
I know Googling I can find an appropriate answer, but I prefer listening to your personal (and maybe technical) opinions.
What is the main reason of the difference between Java and C# in throwing exceptions?
In Java the signature of a method that throws an exception has to use the "throws" keyword, while in C# you don't know in compilation time if an exception could be thrown.
推荐答案
因为对检查异常的响应几乎总是:
Because the response to checked exceptions is almost always:
try {
// exception throwing code
} catch(Exception e) {
// either
log.error("Error fooing bar",e);
// OR
throw new RuntimeException(e);
}
如果你真的知道如果抛出一个特定的异常你可以做些什么,那么你可以捕获它然后处理它,否则它只是安抚编译器的咒语.
If you actually know that there is something you can do if a particular exception is thrown, then you can catch it and then handle it, but otherwise it's just incantations to appease the compiler.
这篇关于为什么在 .NET 中不检查异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!