CSharp · Console
using System;
using Polly;
using Polly.Fallback;
namespace MatchExceptionType
{
public class Program
{
public static void Main(string[] args)
{
Type exceptionType = Type.GetType("System.DivideByZeroException");
FallbackPolicy<string> policy = Policy<String>
.Handle<Exception>(e => exceptionType.IsAssignableFrom(e.GetType()))
.Fallback(e => "Policy handled a DivideByZeroException");
String result = policy.Execute(() => { throw new DivideByZeroException();});
Console.WriteLine(result);
try {
string discarded = policy.Execute(() => { throw new OverflowException();}); // Policy won't handle this.
}
catch
{
Console.WriteLine("As expected, Policy didn't handle an OverflowException.");
}
}
}
}