sonarqube的扫描结果提示

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/creating-and-throwing-exceptions

https://stackoverflow.com/questions/22453650/why-are-we-not-to-throw-these-exceptions

Exception is the base type for all exceptions, and as such terribly unspecific. You shouldn’t ever throw this exception because it simply does not contain any useful information. Calling code catching for exceptions couldn’t disambiguate the intentionally thrown exception (from your logic) from other system exceptions that are entirely undesired and point out real faults.

The same reason also applies to SystemException. If you look at the list of derived types, you can see a huge number of other exceptions with very different semantics.

NullReferenceException and IndexOutOfRangeException are of a different kind. Now these are very specific exceptions, so throwing them could be fine. However, you still won’t want to throw these, as they usually mean that there are some actual mistakes in your logic. For example the null reference exception means that you are trying to access a member of an object which is null. If that’s a possibility in your code, then you should always explicitly check for null and throw a more useful exception instead (for example ArgumentNullException). Similarly, IndexOutOfRangeExceptions occur when you access an invalid index (on arrays—not lists). You should always make sure that you don’t do that in the first place and check the boundaries of e.g. an array first.

There are a few other exceptions like those two, for example InvalidCastException or DivideByZeroException, which are thrown for specific faults in your code and usually mean that you are doing something wrong or you are not checking for some invalid values first. By throwing them knowingly from your code, you are just making it harder for the calling code to determine whether they were thrown due some fault in the code, or just because you decided to reuse them for something in your implementation.

Of course, there are some exceptions (hah) to these rules. If you are building something that may cause an exception which exactly matches an existing one, then feel free to use that, especially if you are trying to match some built-in behavior. Just make sure you choose a very specific exception type then.

In general though, unless you find a (specific) exception that fills your need, you should always consider creating your own exception types for specific expected exceptions. Especially when you are writing library code, this can be very useful to separate the exception sources.

Defining Exception Classes

Programs can throw a predefined exception class in the System namespace (except where previously noted), or create their own exception classes by deriving from Exception. The derived classes should define at least four constructors: one default constructor, one that sets the message property, and one that sets both the Message and InnerException properties. The fourth constructor is used to serialize the exception. New exception classes should be serializable. For example:

[Serializable()]
public class InvalidDepartmentException : System.Exception
{
public InvalidDepartmentException() : base() { }
public InvalidDepartmentException(string message) : base(message) { }
public InvalidDepartmentException(string message, System.Exception inner) : base(message, inner) { } // A constructor is needed for serialization when an
// exception propagates from a remoting server to the client.
protected InvalidDepartmentException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}

New properties should only be added to the exception class when the data they provide is useful to resolving the exception. If new properties are added to the derived exception class, ToString() should be overridden to return the added information.

最新文章

  1. spoj 371 Boxes
  2. 关于在DataGrid.RowDetailsTemplate中的控件查找不到的问题
  3. 关于swap
  4. 《Java多线程核心技术》读书摘要
  5. ./configure --prefix=
  6. openstack api users list get token get servers
  7. js写分页
  8. Rouh set 入门知识2(基础定义篇)
  9. POJ3080 Blue Jeans
  10. asp.net中Repeart选中整行操作
  11. WINDOWS API 函数(超长,值得学习)
  12. accept功能
  13. Cookie 的设置和获取
  14. Spring MVC 关于分页的简单实现
  15. (转载)RESTful架构风格下的4大常见安全问题
  16. html基本标签与属性
  17. css3 动画 总结
  18. 解决flask中文乱码的问题
  19. 查询Oracle 临时表空间使用情况[z]
  20. 从零搭建ES搜索服务(一)基本概念及环境搭建

热门文章

  1. 点击不同按钮,加载不同的页面(不使用iframe的情况下)
  2. jvm gc回收机制
  3. Oracle外键级联删除和级联更新
  4. hdu - 3594 Cactus (强连通)
  5. CAN 和 CANopen的区别和联系
  6. mybatis注解@selectKey对于db2数据库的使用
  7. 前端进阶之路:初涉Less
  8. C++ OCX控件开发后出现的注册问题
  9. Java并发编程-Executor框架(转)
  10. Python--学习过程