Return in catch block?

C#.NetException Handling

C# Problem Overview


Is is wrong to have a return statement in a catch block? What are the alternatives?
i.e:

public bool SomeFunction()
{
    try
    {
        //somecode
        return true;
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.message);
        return false;
    }
    
}

C# Solutions


Solution 1 - C#

You can return normally from catch block. It's normally good functional code.

Solution 2 - C#

One alternative would be to store the return value in a temporary variable:

public bool SomeFunction()
{
    bool success = true;
    try
    {
        //somecode
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.message);
        success = false;
    }

    return success;
}

But personally, I find the way you've written it (with one catch-all catch statement) to be more readable. On the other hand, if you are expecting a specific exception and you might have multiple paths to return success or not...

try
{
    DoTheImportantThing();
    DoTheOtherThingThatMightFailButWeDontCare();
}
catch (DontCareAboutItException ex)
{
    log.Info(ex);
}
catch (Exception ex)
{
    log.Error(ex);
    return false;
}

return true;

Then in my opinion you're best off pushing the return statements as close to the end as possible.

As a side note, depending on the application, consider logging the exceptions you catch rather than just showing them to the user. Logged exceptions are a lot more reliable than user's recounts of what happened.

Solution 3 - C#

If in the try block there's already a return statement I would probably put the other return at the end of the function:

try
{
    //somecode
    return true;
}
catch(Exception ex)
{
    MessageBox.Show(ex.message);
}
return false;

And this in order to avoid multiple returns if multiple exceptions need to be handled.

Solution 4 - C#

It's ok, just keep in mind, that some code may executed after return instruction (return value will be cashed).

    try
    {
        return;
    }
    catch(Exception ex)
    {
        return;
    }
    finally
    {
        //some code
    }

Solution 5 - C#

public bool SomeFunction()
{
    try
    {
        //somecode
        return true;
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.message);
    }
    return false;
}

Personally I put the return statement at the bottom of the method instead of in the catch-block. But both are fine. It's all about readability (subjective) and guidelines in your organization.

Solution 6 - C#

Yes, it's perfectly normal.

Don't forget, that you can also use finally block to be executed after return.

Solution 7 - C#

It isn't wrong, but if you used a resource, generally finally block is used to close it, instead of calling the close method twice, too. In that case, you may choose to use the return statement after the finally block.

Solution 8 - C#

Makes perfect sense to me for a function which returns true on success and false on failure. I hope there is nothing wrong with it - I do it all the time :)

Solution 9 - C#

You can add a return in the catch block. You explicitly know that your code will return and continue execution instead of halting at the catch block.

try{
    //do something
}
catch{
    //error handling
    return;
}

Instead of having a lot of try-catch blocks in your code that can get confusing and just cause a mess in your code, it's better handle everything in your noe try catch and just check what the error returned was.

try{
    //do something
}
catch (Exception as err){
    if (err == "IOException"){
         //handle exception
    else if (err.indexOf("TypeError"){
         //handle exception
    }
}

These are two ways of checking what type the exception was so you can display a message accordingly. You can also just catch specific exceptions if you wanted so instead of Exception as err you can do catch IOException, ...

Solution 10 - C#

The primary purpose of a catch block is to provide a place where one can proceed an exceptional situation which occurred in a try block. So you caught an exception, proceeded it... and returned from a method. If the caller method does not care about the exception, this is absolutely normal.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlowlyinternView Question on Stackoverflow
Solution 1 - C#SvisstackView Answer on Stackoverflow
Solution 2 - C#Mark RushakoffView Answer on Stackoverflow
Solution 3 - C#Darin DimitrovView Answer on Stackoverflow
Solution 4 - C#AndrewView Answer on Stackoverflow
Solution 5 - C#BartView Answer on Stackoverflow
Solution 6 - C#Piotr JustynaView Answer on Stackoverflow
Solution 7 - C#FeyyazView Answer on Stackoverflow
Solution 8 - C#RayView Answer on Stackoverflow
Solution 9 - C#E. OregelView Answer on Stackoverflow
Solution 10 - C#n535View Answer on Stackoverflow