Tuesday 15 April 2014

exception handling - Why does C# allow you to 'throw null'? -


While writing some special complicated exception handling codes, someone asked, you do not have to make sure that your exception object ' T tap? And I said, of course not, but decided to try again. Obviously, you can throw the trapped, but it's still turned into an exception.

Why is this allowed?

  throwing taps;   

In this snippet, thankfully 'east' is not zero, but can it ever be?

  try {Throwing; } Hold (Exception Pre) {// Can Ever Be Ever? // thank you, this is not empty, but / / is EX / System.NullReferenceException}    

Because the language specification is expected to manifest System.Exception (hence, zero is valid in that context) and this expression does not restrict the non-zero . In general, there is no way that it can detect whether the value of that expression is null or it should solve the problem of preventing the runtime from the null case Will have to deal with in any way. See:

  exception ex = null; If (conditionThatDependsOnSomeInput) ex = new exception (); East;   

They can, of course, make a specific case of throwing illegal , but it will not help very much, so why waste the specification location and stability Reduce for a little profit?

Disclaimer (before I slapped it by Eric Lippert): There are speculations about the logic behind this design decision My Of course, I have not been in a design meeting Answer: Answer to your second question, even if the expression clerk caught within the catch clause can also be empty: while the C # specification is silent about whether Exceptions can be made to null in other languages It defines the way in which exceptions are propagated:

The catch segment, if any, is examined in order to present an appropriate handler for the exception. . The first catch class which specifies the type of exception type or exception type is considered a match, a common catch clause is considered to be a match for an exception type. [...]

For the null , the bold statement is incorrect, therefore, while according to C # spec purely, we can not say that the underlying Runtime will never throw the null, we can make sure that this is the case, even if this is the case, this is the only normal hold {} section section

CI on C # For implementation, we can refer to the ECMA 335 specification. This document defines all exceptions that CLI throws internally (none of these is null ) and it mentions that the user defined exception object is throw The instruction that is thrown by the instruction is similar to the statement of C # throw statement (except that it does not restrict the type of object to System.Exception ):

Description:

throw instruction Exceptions Tak object (type throws O ) and the stack is empty. For details of the exception mechanism, see Division I. [NOTE: When CII allows to throw any object, CLS describes a specific exception class that will be used for language differences. End note]

Exception:

System.NullReferenceException is thrown if obj is null .

Correct:

The correct CIL ensures that the object is always null or the object reference (that is, type O ) < / P> >

No comments:

Post a Comment