yohhoyの日記

技術的メモをしていきたい日記

throw null;(C#編)

プログラミング言語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 type System.Exception or of a class type that derives from System.Exception. If evaluation of the expression produces null, a System.NullReferenceException is thrown instead.
(snip)

関連URL