Search code examples
c#exceptionprogram-flow

Program flow after an exception is thrown in C#


Hi I'm looking at some old c# code and noticing a lot of code like this:

void SomeFunction()
{
    if (key.Length != Dimensions)
    {
        throw new KeySizeException();
    }
    else
    {
        SomeOtherFunction();
    }
}

I want to know if there can ever be a case where the else block is even necessary? Can I safely shorten the code to this with no repercussions?

void SomeFunction()
{
    if (key.Length != Dimensions)
    {
        throw new KeySizeException();
    }

    SomeOtherFunction();
}

By default the exception should throw the program flow out of this method right? But I'm just wondering if there's a way in DotNet to tweak how unhandled exceptions are um handled, that would cause the 2nd implementation to work differently from the first?


Solution

  • You do not need the 'else' block. It is redundant. If you use a refactoring tool like 'Reshaper' or 'JustCode' such redundant code elements are usually pointed out.