C#编译器缺陷?:不检测总是抛出异常的方法
发布时间:2020-12-15 08:33:00 所属栏目:百科 来源:网络整理
导读:为什么MS C#编译器在以下场景中抱怨“并非所有代码路径都返回值”? public int Foo(bool flag){ if(flag) { return 1; } else { ThrowException(); // this method always throws an exception // return -1; // why do I need to add this code that will
为什么MS C#编译器在以下场景中抱怨“并非所有代码路径都返回值”?
public int Foo(bool flag) { if(flag) { return 1; } else { ThrowException(); // this method always throws an exception // return -1; // why do I need to add this code that will never be called? } } 谢谢! 解决方法
它无法猜测ThrowException()是一个总是抛出异常的方法.为此,您需要静态代码分析.
VS 2010中提供了静态代码分析,但我认为仅适用于更昂贵的VS版本. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |