Supplemental claim for catch statement - code smell?

Situation:

My application should handle the first step in business rules (the initial try-catch statement). If a certain error occurs when calling a helper method during a step, I need to switch to the second process in the catch statement. The backup process uses the same helper method. If the same error occurs during the second process, I need to stop the whole process and throw an exception.

Implementation:

I was about to insert another statement, the try-catchstatement of the catchfirst statement try-catch.

//run initial process
try
{
    //initial information used in helper method
    string s1 = "value 1";

    //call helper method
    HelperMethod(s1);
}
catch(Exception e1)
{
    //backup information if first process generates an exception in the helper method
    string s2 = "value 2";

    //try catch statement for second process.
    try
    {
        HelperMethod(s2);
    }
    catch(Exception e2)
    {
        throw e2;
    }
}

What will be the correct design pattern to avoid code smells in this implementation?

, . , .

+5
15

HelperMethod , , catch , stacktrace e2.

:

try
{
    //call helper method
    HelperMethod();
}    
catch(Exception e1)
{
    // maybe log e1, it is getting lost here
    HelperMethod();
}
+12

, , , . , , - , Exception. , , Dispose() , (WCF, ).

+4

, try-catch, . - , , , - (, ). , : 1) Wim 2) , ( e1)? , , .

+2

, , , .

, (, , ). , , .

+1

, , , int attempt, .

   private void MyMethod (parameterList)
   {  MyMethod(ParameterList, 0)l }

   private void MyMethod(ParameterList, int attempt)
   {
      try { HelperMethod(); }
      catch(SomeSpecificException)
      {
          if (attempt < MAXATTEMPTS)
              MyMethod(ParameterList, ++attempt);
          else throw;
      }
   }
+1

. , , .

0

, , , ..

0

, catch, , , . , , .

throw e2; : throw;, , . , /.

e1, catch (Exception) catch (YourSpecificException)

0

:

// set up state for first attempt
if(!HelperMethod(false)) {
        // set up state for second attempt
        HelperMethod(true);
        // no need to try catch since you're just throwing anyway
}

HelperMethod

bool HelperMethod(bool throwOnFailure)

, (.. false true ). :

// could wrap in try/catch
HelperMethod(2, stateChanger);

HelperMethod -

void HelperMethod(int numberOfTries, StateChanger[] stateChanger)

numberOfTries , StateChanger[] - , (.. stateChanger[0] , stateChanger[1] ..)

, . , , , ( ), (HelperMethod). SRP .

, catch , ( Exception!), throw throw e, , ..

0

, - , , .

, , SQL Server, , , , .

- , .

, , , , , , . , , (, ).

, , , . , , , SqlException, , , , SQL- , .

, , " " - , () 1 , (, ) 90 . .

, try/catch.

:

bool helper_success = false; 
bool automatic_retry = false; 
//run initial process
try
{
    //call helper method
    HelperMethod();
    helper_success = true; 
}
catch(Exception e)
{
    // check if e is a transient exception. If so, set automatic_retry = true 
} 


if (automatic_retry)
{    //try catch statement for second process.
    try
    {
        HelperMethod();
    }
    catch(Exception e)
    {
        throw;
    }
}
0

# TryParse:

class Program
{
    static void Main(string[] args)
    {
        Exception ex;
        Console.WriteLine("trying 'ex'");
        if (TryHelper("ex", out ex))
        {
            Console.WriteLine("'ex' worked");
        }
        else
        {
            Console.WriteLine("'ex' failed: " + ex.Message);
            Console.WriteLine("trying 'test'");
            if (TryHelper("test", out ex))
            {
                Console.WriteLine("'test' worked");
            }
            else
            {
                Console.WriteLine("'test' failed: " + ex.Message);
                throw ex;
            }
        }
    }

    private static bool TryHelper(string s, out Exception result)
    {
        try
        {
            HelperMethod(s);
            result = null;
            return true;
        }
        catch (Exception ex)
        {
            // log here to preserve stack trace
            result = ex;
            return false;
        }
    }

    private static void HelperMethod(string s)
    {
        if (s.Equals("ex"))
        {
            throw new Exception("s can be anything except 'ex'");
        }
    }

}
0

try/catch, , - API :

public void Foo()
{
  try
  {
    HelperMethod("value 1");
    return; // finished
  }
  catch (Exception e)
  {
     // possibly log exception
  }

  try
  {
    HelperMethod("value 2");
    return; // finished
  }
  catch (Exception e)
  {
     // possibly log exception
  }

  // ... more here if needed
}
0

( , , ), goto. # , .

const int MAX_RETRY = 3;

public static void DoWork()
{
    //Do Something
}

public static void DoWorkWithRetry()
{
    var @try = 0;
retry:
    try
    {
        DoWork();
    }
    catch (Exception)
    {
        @try++;
        if (@try < MAX_RETRY)
            goto retry;
        throw;
    }
}
0

, "", , , .

//run initial process
try
{
    //initial information used in helper method
    string s1 = "value 1";

    //call helper method
    if(!HelperMethod(s1))
    {
        //backup information if first process generates an exception in the helper method
        string s2 = "value 2";
        if(!HelperMethod(s2))
        {
          return ErrorOfSomeKind;
        }
    }
    return Ok;
}
catch(ApplicationException ex)
{
    throw;
}
0

, try catch , - ( json-, base64), , .

- , .

, ( ).

, try , (, ) , .

0
source

All Articles