exception handling - Why does C# allow you to 'throw null'? -
While writing some specific 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 tap, but it has 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, it is not zero, but / / is EX / System.NullReferenceException}
Because the language specification is expected to manifest System.Exception
(hence, null
is valid in that context) and does not restrict this expression of 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, why waste the specification 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: The answer to your second question, whether an expression varial caught inside the catch clause can ever be empty: however the C # specification is silent about that. Whether the null
exception in other languages can be promoted It defines the way to promote exceptions:
The catch segment, if any are checked in order to be present to detect an appropriate handler for the exception. is. 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 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
permissions Stack exception object (TypeO ) 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 ifobj
isnull
.Accuracy:
The correct CIL ensures that the object is always
>null
or object reference (i.e., typeO
) < / P>
Comments
Post a Comment