为什么不例外在.NET中选中?NET

2023-09-02 10:22:18 作者:我是个真男人!

我知道,谷歌搜索我能找到一个合适的答案,但我preFER听你的个人(也许技术)的意见。 什么是Java和C#的区别在抛出异常的主要原因是什么? 在Java中抛出异常的方法的签名必须使用抛出的关键字,而在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.

 
精彩推荐
图片推荐