How to be explicit about NOT throwing an exception?

This may be a broad question, but recently I thought about the following: on our C # -component server we have many places that transfer some code to the try/ block catch, in particular, it calls external WcF services. Some of these calls are crucial for the application, so in the catch block we register an error and a reversal, for example:

catch(Exception ex)
{
    _logger.Error("Some good error message");
    throw ex;
}

On the other hand, there are services that we allow the failure, but we still want to register an error, so they look like this:

catch(Exception ex)
{
    _logger.Error("Some good error message");
}

Now, having read the code of the team members, I can’t be sure that they forgot to quit or this is the intended behavior.

Q: Is there a way, resp. which is the default method, DO NOT explicitly revert (without including a comment in the code).

- :

catch(Exception ex)
{
    _logger.Error("Some good error message");
    NotThrowingHereOnPurpose();
}

// ...
// and further below a private method
// ...

private void NotThrowingHereOnPurpose(){}
+4
5

, , , : . , , :

public void MethodThatFailsSometimes()
{
    try {
        PerformFailingOperation();
    } 
    catch (Exception e) when (e.LogAndBeCaught())
    {
    }
}

Exception , LogAndBeCaught LogAndEscape:

public static bool LogAndBeCaught(this Exception e)
{
    _logger.Error(@"Following exception was thrown: {e}");
    return true;
} 

public static bool LogAndEscape(this Exception e)
{
    _logger.Error(@"Following exception was thrown: {e}");
    return false;
} 
0

, , , , , , try/catch.

, , , , lambdas:

void InvokeFailSafe(Action action, Action<Exception> onFailure = null) {
    try {
        action();
    } catch (Exception e) {
        if (onFailure != null) {
            onFailure(e);
        }
    }
}

try/catch :

InvokeFailSafe(
    () => {
        ... The code that may fail
    }
,   exception => _logger.Error("Some good error message: {0}", exception)
);

, , - :

InvokeFailSafe(
    () => {
        ... The code that may fail
    }
);

, throw.

+3

dasblinkenlight. , , , , .

, Error, . .

ExceptionDispatchInfo. ExceptionDispatchInfo . throw; ( ).

public static void ErrorAndThrow(this ILogger logger, string message, Exception exception)
{
    var exceptionInfo = ExceptionDispatchInfo.Capture(exception);

    logger.Error(message);
    exceptionInfo.Throw();
}

:

try
{
}
catch (Exception ex)
{
    // ex would be rethrown here
    _logger.ErrorAndThrow("Some good error message", ex);
}
+1

Q: , . , rethrow ( ).

- . , , . , , , . , . , , , .

, , , .

, . . . , , .

.

0

It simply should not occur very often. Consider

string WebGreeting()
{
  try
  {
     return _greetingService.HelloWorld();
  }
  catch(Exception ex)
  {
      _logger.Error("Some good error message");
      //throw;  // when this is missing, you get the compiler error "not all paths ..."
  }
  // unless you very deliberately code the on-error case
  // return "nothing";
}

So that leaves an odd method void. But is this really your question?

-1
source

Source: https://habr.com/ru/post/1684300/


All Articles