相关疑难解决方法(0)

如何重新抛出InnerException而不会丢失C#中的堆栈跟踪?

我通过反射调用一种可能导致异常的方法.如何在没有包装器反射的情况下将异常传递给调用者?
我正在重新抛出InnerException,但这会破坏堆栈跟踪.
示例代码:

public void test1()
{
    // Throw an exception for testing purposes
    throw new ArgumentException("test1");
}

void test2()
{
    try
    {
        MethodInfo mi = typeof(Program).GetMethod("test1");
        mi.Invoke(this, null);
    }
    catch (TargetInvocationException tiex)
    {
        // Throw the new exception
        throw tiex.InnerException;
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# exception

290
推荐指数
9
解决办法
7万
查看次数

Polly政策记录异常和重新抛出

我考虑使用 Polly 来创建记录异常和重新抛出的策略.我没有找到允许它开箱即用的现有方法,但我看到的一些选项

倒退

// Specify a substitute value or func, calling an action (eg for logging) if the fallback is invoked.
Policy.Handle<Whatever>()  
 .Fallback<UserAvatar>(UserAvatar.Blank, onFallback: (exception, context) =>  
   {   _logger.Log(exception, context);
       throw exception;
  });
Run Code Online (Sandbox Code Playgroud)

问题:从Fallback抛出异常是否可以?

超时

Policy.Timeout(1, T30meoutStrategy.Pessimistic, (context, timespan, task) =>  
   {        task.ContinueWith(t =>
 { // ContinueWith important!: the abandoned task may very well still be executing, when the caller times out on waiting for it!    
   if (t.IsFaulted      )           
  {             
       logger.Error(context,t.Exception);         
       throw exception;
  }   );    
Run Code Online (Sandbox Code Playgroud)

或者重试

Policy.Handle<DivideByZeroException>().Retry(0, (exception, …
Run Code Online (Sandbox Code Playgroud)

c# exception-handling polly

5
推荐指数
2
解决办法
5952
查看次数

执行多个策略

如何执行多个策略(或将它们合并为一个策略)?

例如我有:

var policy1 = Policy.Handle< DivideByZeroException >().WaitAndRetry(5));

var policy2 = Policy.Handle< StackOverflowException >().RetryForever();
Run Code Online (Sandbox Code Playgroud)

如何同时将它们应用于一种方法?

.net polly

1
推荐指数
1
解决办法
903
查看次数

标签 统计

.net ×2

c# ×2

polly ×2

exception ×1

exception-handling ×1