为什么C#允许你'扔空“? [英] Why does C# allow you to 'throw null'?
问题描述
在写一些特别复杂的异常处理代码,有人问,难道你需要确保你的异常对象不是null?我说,当然不是,但后来决定试一试。显然,你可以扔掉空,但它仍然是变成了一个异常的地方。
这是为什么不允许?
掷空;
在这个片段中,值得庆幸的是'前'不为空,但它可能永远不会?
试
{
掷空;
}
赶上(异常前)
{
//可以永远前为空?
//幸运的是,它不为空,但
//当然是System.NullReferenceException
}
由于语言规范期望类型的表达式 System.Exception的
那里(因此,空
在这种背景下有效),并不会限制该表达式非空。一般情况下,有没有办法,它可以检测表达式的值是否为空
与否。它必须解决停机问题。运行时将反正对付空
情况。参见:
异常前= NULL;
如果(conditionThatDependsOnSomeInput)
前=新的异常();
罚球前;
他们当然可以,使投掷的特定情况下的空
文字无效,但是这将帮助不大,那么又何必浪费规范空间和减少小利益一致性?
<子>免责声明(之前我得到由埃里克利珀耳光):这是的我自己的这个设计决定背后的原因猜测。当然,我还没有在设计会议;)
回答你的第二个问题无论catch子句中捕获一个表达式变量都不能为空:虽然C#规范是闭口不谈其他语言是否会导致空
例外被传播,它定义例外的方式传播:
的catch子句,如果有的话,在出现的顺序,以找到合适的异常处理程序检查。第一个catch子句的指定异常类型或异常类型的基本类型被认为是匹配的。一般的catch子句被认为是任何异常类型的匹配。 [...]
块引用>
有关
空
中,大胆的声明是假的。所以,虽然纯粹基于C#规范说什么,不能说的底层运行时永远不会丢空,我们可以肯定的是,即使是这样的话,它会只能由普通的<$ C处理$ C>捕捉{} 条款。
有关CLI的C#实现,大家可以参考ECMA 335规范。该文件定义了CLI内部抛出的所有异常(其中没有一个是
空
),并提到,用户定义的异常对象由扔抛出code>指令。该指令的描述几乎是相同的C#
罚球
语句(除非它不限制对象的类型的System.Exception
):
说明:
的
罚球
指令抛出堆栈上的异常对象(类型0
),并清空堆栈。对于异常机制的详细信息,请参阅分区I.结果
[注:虽然CLI允许抛出的对象时,CLS描述了应当用于语言互操作性特定的异常类。注完]
例外:
System.NullReferenceException
被抛出,如果OBJ
是空
。
正确性
正确CIL确保对象总是要么
空
或对象引用(即类型的0
)。
块引用>
我相信这些都足以断定捕获的异常是绝不会
空
。While writing some particularly complex exception handling code, someone asked, don't you need to make sure that your exception object isn't null? And I said, of course not, but then decided to try it. Apparently, you can throw null, but it is still turned into an exception somewhere.
Why is this allowed?
throw null;
In this snippet, thankfully 'ex' is not null, but could it ever be?
try { throw null; } catch (Exception ex) { //can ex ever be null? //thankfully, it isn't null, but is //ex is System.NullReferenceException }
解决方案Because the language specification expects an expression of type
System.Exception
there (therefore,null
is a valid in that context) and doesn't restrict this expression to be non-null. In general, there's no way it could detect whether the value of that expression isnull
or not. It would have to solve the halting problem. The runtime will have to deal with thenull
case anyway. See:Exception ex = null; if (conditionThatDependsOnSomeInput) ex = new Exception(); throw ex;
They could, of course, make the specific case of throwing the
null
literal invalid but that wouldn't help much, so why waste specification space and reduce consistency for little benefit?Disclaimer (before I get slapped by Eric Lippert): This is my own speculation about the reasoning behind this design decision. Of course, I haven't been in the design meeting ;)
The answer to your second question, whether an expression variable caught within a catch clause can ever be null: While the C# specification is silent about whether other languages can cause a
null
exception to be propagated, it does define the way exceptions are propagated:The catch clauses, if any, are examined in order of appearance to locate a suitable handler for the exception. The first catch clause that specifies the exception type or a base type of the exception type is considered a match. A general catch clause is considered a match for any exception type. [...]
For
null
, the bold statement is false. So, while purely based on what the C# spec says, we can't say the underlying runtime won't ever throw null, we can be sure that even if that's the case, it'll be only handled by the genericcatch {}
clause.For C# implementations on the CLI, we can refer to the ECMA 335 specification. That document defines all exceptions that the CLI throws internally (none of which are
null
) and mentions that user defined exception objects are thrown by thethrow
instruction. The description for that instruction is virtually identical to C#throw
statement (except that it doesn't restrict the type of the object toSystem.Exception
):Description:
The
throw
instruction throws the exception object (typeO
) on the stack and empties the stack. For details of the exception mechanism, see Partition I.
[Note: While the CLI permits any object to be thrown, the CLS describes a specific exception class that shall be used for language interoperability. end note]Exceptions:
System.NullReferenceException
is thrown ifobj
isnull
.Correctness:
Correct CIL ensures that object is always either
null
or an object reference (i.e., of typeO
).I believe these are sufficient to conclude caught exceptions are never
null
.这篇关于为什么C#允许你'扔空“?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!