using System.Threading.Tasks;
public static void Main()
var task1 = Task.Run( () => { throw new CustomException("This exception is expected!"); } );
catch (AggregateException ae)
Console.WriteLine($"ErrorMessage={ae.Message + Environment.NewLine}" +
$"ErrorStackTrace={ae.StackTrace + Environment.NewLine}" +
$"Error InnerException StackTrace={ae?.InnerException?.StackTrace + Environment.NewLine}");
ae.Handle(ex => { if (ex is CustomException)
Console.WriteLine(ex.Message);
return ex is CustomException;
public class CustomException : Exception
public CustomException(String message) : base(message)