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.

Do not throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException intentionally from your own source code的更多相关文章

  1. 类库探源——System.Exception

    一.MSDN描述 Exception 类: 表示在应用程序执行期间发生的错误 命名空间 : System 程序集:   mscorlib.dll 继承关系: 常用属性(含字段)和方法: 1. 属性Me ...

  2. 编写高质量代码改善C#程序的157个建议——建议68:从System.Exception或其他常见的基本异常中派生异常

    建议68:从System.Exception或其他常见的基本异常中派生异常 微软建议:从System.Exception或其他常见基本异常之一派生异常.在Visual Studio中输入Excepti ...

  3. System.Security.SecurityException The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception

    [15/08/19 00:03:10] [DataManager-7292-ERROR] System.Reflection.TargetInvocationException: Exception ...

  4. Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V

    最近下载一个新版本的adt-bundle,Android API是20. 把Plain Text控件往布局上面拖时,发现拖不上去,出现了下面的错误: Exception raised during r ...

  5. ECLIPSE android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V

    在布局添加控件手动添加还是拖的添加,添加edittext后布局就不好用,其他控件好用,然后就说下面这段话 Exception raised during rendering: java.lang.Sy ...

  6. “System.Exception: System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本” 的解决方案

    在项目部署过程中ORACLE客户端多次会遇"System.Exception: System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本&qu ...

  7. “System.Exception”类型的异常在 NHibernate.dll 中发生,但未在用户代码中进行处理

    “System.Exception”类型的异常在 NHibernate.dll 中发生,但未在用户代码中进行处理 其他信息: OCIEnvCreate 失败,返回代码为 -,但错误消息文本不可用. 如 ...

  8. android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V

    今天在看布局文件的时候出现 android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[ ...

  9. DataGridView 中发生以下异常: System.Exception: 是 不是 Decimal 的有效值。 ---> System.FormatException: 输入字符串的格式不正确。

    其实之前我自己是没测出这个问题的,但是一放到测试的手上就出来了,原因我知道在哪里改输什么东西,但是人家不知道啊.报错如下: --------------------------- “DataGridV ...

随机推荐

  1. jvm gc回收机制

    jdk8取消永久代,增加metaspace元空间.使用本地内存,不使用堆内存. jstat -gcutil jstat -gccause pid 1 每格1毫秒输出结果jstat -gccause p ...

  2. 查看mysql库中所有表的大小和记录数

    查看mysql库中所有表的大小和记录数 ,), 'MB') as total_size FROM information_schema.TABLES WHERE TABLE_SCHEMA='datab ...

  3. android:logo

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  4. delphi使用IdHTTP模拟提交页面方法总结

    http://blog.csdn.net/lxdcyh/article/details/3986800 1.拖入TIdHTTP控件,HandleRedirect设为True,否则可能会出现HTTP 3 ...

  5. Heavy Transportation(最短路)

    poj 1797 ——Heavy Transportation 思路: 这道题我们可以采用类似于求最短路径的方法,用一种新的“松弛操作”去取代原本的方法. 我们可以记录d[u]为运送货物到点j时最大可 ...

  6. JStorm学习

    一.简介 JStorm是一个分布式实时计算引擎.JStorm是一个类似于Hadoop MapReduce的系统,用户按照指定的接口实现一个任务,然后将这个任务交给JStorm系统,JStorm将这个任 ...

  7. sql-trace-10046-trcsess-and-tkprof

    https://oracle-base.com/articles/misc/sql-trace-10046-trcsess-and-tkprof awr/statspack,sqlprofile,sq ...

  8. 前端MVC Vue2学习总结(八)——前端路由

    路由是根据不同的 url 地址展示不同的内容或页面,早期的路由都是后端直接根据 url 来 reload 页面实现的,即后端控制路由. 后来页面越来越复杂,服务器压力越来越大,随着AJAX(异步刷新技 ...

  9. uicollectionview 使用uibutton或者uiimageview实现旋转出现scale的问题

    uicollectionview下单独使用uibutton然后setimage或者直接使用uiimageview然后一定角度旋转后发现size会变动 解决方案:添加uibutton到uicollect ...

  10. 自动调整文字高度With what should I replace the deprecated sizeWithFont:contrainedToSize:lineBreakMode method?

    自动调整文字的高度: ios 2.0 ~ 7.0以下: UILabel *orgnizationLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, ...