Program flow after exception in C #

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?

+3
source share
5 answers

"else". . , Reshaper '' JustCode ' .

+5

throw - , . , else .

+3

.

+1

# . , , ( ), ?

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(); 
} 
+1

, .

, .

-, , , , - (SomeFunction), guard SomeOtherFunction. - KeySizeException , , SomeOtherFunction . , SomeOtherFunction .

, .Net 4.0 Code Contracts - .

- , , . 100% - , if/else.

0

All Articles