是否有 .Net Exception 类型列表以及何时使用它们的建议?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6676021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:43:29  来源:igfitidea点击:

Is there a list of .Net Exception types and advice on when to use them?

.netexception

提问by Calanus

Does anyone know of a list of .Net Exception types .e.g. ArgumentNullExceptionand under what circumstances you should use them? At the moment I'm just guessing based on the type name but I would rather get it right.

有谁知道 .Net Exception 类型列表 .egArgumentNullException以及在什么情况下应该使用它们?目前我只是根据类型名称进行猜测,但我宁愿做对。

采纳答案by Adam Houldsworth

A list of them:

他们的名单:

http://mikevallotton.wordpress.com/2009/07/08/net-exceptions-all-of-them/

http://mikevallotton.wordpress.com/2009/07/08/net-exceptions-all-of-them/

As for the circumstance, it depends, most of the time they make sense based on their name and arguments. ArgumentNullException, for example, is usually used when checking method / constructor arguments for null values.

至于具体情况,视情况而定,大多数情况下,根据名称和参数,它们是有意义的。 ArgumentNullException例如,通常在检查空值的方法/构造函数参数时使用。

Then you have others such as FileNotFoundException, NullReferenceException, InvalidOperationException, as you can see they read very easily and should be used with common sense.

然后你还有其他的,比如FileNotFoundException, NullReferenceException, InvalidOperationException,你可以看到它们很容易阅读,应该按照常识使用。

Update 1:as someone has suggested, they are also on MSDN:

更新 1:正如有人建议的那样,他们也在 MSDN 上:

http://msdn.microsoft.com/en-us/library/system.exception.aspx#inheritanceContinued

http://msdn.microsoft.com/en-us/library/system.exception.aspx#inheritanceContinued

Here are views on common exceptions available:

以下是对常见异常的看法:

https://blogs.msmvps.com/jgaylord/2009/07/08/common-and-all-system-exceptions-in-net/

https://blogs.msmvps.com/jgaylord/2009/07/08/common-and-all-system-exceptions-in-net/

http://www.developerfusion.com/article/1889/exception-handling-in-c/3/

http://www.developerfusion.com/article/1889/exception-handling-in-c/3/

Update 2:as for usage, Microsoft has guidelines for reserved exception types:

更新 2:至于用法,Microsoft 有保留异常类型的指南:

http://msdn.microsoft.com/en-us/library/ms182338.aspx

http://msdn.microsoft.com/en-us/library/ms182338.aspx