function that recalls itself upon a catch of non critical error

I got a function named inner_logic() that can throw many types of exception , each of one of two types: critical and non-critical. The logic of inner_logic() includes an "infinite" loop, so in case of a non-critical exception I'd wish to step back in to it. At that point you can ask why not catching the non-critical exception within the loop, and the reason is that it's an abstract class with a few inheritors, each have a unique loop. for clarification, what I want to do is something like:

public void logic()
{
try
{
 inner_logic();
}
catch (non_critical e)
{
  // log 
  logic();
}
catch(critical e)
{
 // log 
 return;
}

public virtual void inner_logic(){}

As far as I can tell, I seems that it should work, and that all calls are "catchable", but it feels kinda hackish.

My Question is: Is there a better way for doing that, or a reason I should reconsider my suggestion?

Jon Skeet
people
quotationmark

It feels to me like it should just be looping:

while (true) // Or probably have a maximum number of retries
{
    try
    {
        // Logic

        // Break out of the loop by returning if we succeed
        return;
    }
    catch (NonCriticalException e)
    {
        // Log, then let the loop continue
    }
    catch (CriticalException e)
    {
        // Log, then rethrow (in preference to returning)
    }
}

With your original approach, if the non-critical exception just keeps happening, you'll get a StackOverflowException. I would definitely suggest adding a maximum retry count though. (Keeping track of the number of retries is good for logging, too.)

people

See more on this question at Stackoverflow