Hi, I look at the old C # code and notice a lot of code:
void SomeFunction() { if (key.Length != Dimensions) { throw new KeySizeException(); } else { SomeOtherFunction(); } }
I want to know if there might be a case where an else block is needed? Can I safely shorten the code to this without any consequences?
void SomeFunction() { if (key.Length != Dimensions) { throw new KeySizeException(); } SomeOtherFunction(); }
By default, an exception should throw a program thread from this method correctly? But I'm just wondering if there is a way in DotNet to configure how unhandled exceptions are handled, which will cause the second implementation to work differently than the first?
"else". . , Reshaper '' JustCode ' .
throw - , . , else .
throw
else
.
# . , , ( ), ?
void SomeFunction() { if (key.Length != Dimensions) { throw new KeySizeException(); //Halt the execution of SomeFunction method } SomeOtherFunction(); }
SomeOtherFunction, , .
void SomeFunction() { if (key.Length != Dimensions) { HandleMyException(); return; // Returns and halt the execution of SomeFunction method. } SomeOtherFunction(); }
, .
-, , , , - (SomeFunction), guard SomeOtherFunction. - KeySizeException , , SomeOtherFunction . , SomeOtherFunction .
, .Net 4.0 Code Contracts - .
- , , . 100% - , if/else.