为什么在 .NET 中不检查异常?

编程入门 行业动态 更新时间:2024-10-25 23:25:44
本文介绍了为什么在 .NET 中不检查异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道使用谷歌搜索我可以找到合适的答案,但我更喜欢听听您的个人(也许是技术)意见.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 中不检查异常?

本文发布于:2023-11-25 13:45:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   异常   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!