1 没有返回值的降级
Policy policy = Policy.Handle<Exception>().Fallback(() => { Console.WriteLine("我是你爸爸"); }); policy.Execute(() => { Console.WriteLine("开始执行"); throw new Exception(); Console.WriteLine("执行结束"); });
2有返回值的降级
Policy<string> policy = Policy<string>.Handle<Exception>().Fallback(() => { Console.WriteLine("苔痕上阶绿"); return "我是希罗多德"; }); string response = policy.Execute(() => { Console.WriteLine("开始执行"); throw new Exception(); Console.WriteLine("执行完毕"); return "我的名字叫做阿基米德"; }); Console.WriteLine(response);