Table of Contents

Sample: IntroExceptionDiagnoser

The ExceptionDiagnoser uses AppDomain.FirstChanceException API to report:

  • Exception frequency: The number of exceptions thrown during the operations divided by the number of operations.

Source code

using BenchmarkDotNet.Attributes;
using System;

namespace BenchmarkDotNet.Samples
{
    [ExceptionDiagnoser]
    public class IntroExceptionDiagnoser
    {
        [Benchmark]
        public void ThrowExceptionRandomly()
        {
            try
            {
                if (new Random().Next(0, 5) > 1)
                {
                    throw new Exception();
                }
            }
            catch
            {
                // ignored
            }
        }
    }
}

Output

Method Mean Error StdDev Exception frequency
ThrowExceptionRandomly 4.936 us 0.1542 us 0.4499 us 0.1381