プログラミング言語C#の throw 文に関するちょっとしたメモ。下記コードはコンパイルエラーとはならず、実際にはSystem.NullReferenceException
オブジェクトが送出される。
throw null; // throw new System.NullReferenceException();と等価
Standard ECMA-334 C# Language Specification(4th Ed.), 15.9.5 The throw statementより該当箇所を引用(下線部は強調)。
The
throw
statement throws an exception.throw-statement:
throw
expressionopt;
A
throw
statement with an expression throws the value produced by evaluating the expression. The expression shall denote a value of the class typeSystem.Exception
or of a class type that derives fromSystem.Exception
. If evaluation of the expression producesnull
, aSystem.NullReferenceException
is thrown instead.
(snip)
関連URL