runtime: [API Proposal]: Let accessing the instance of Exception (if any) on stack.

Background and motivation

From time to time there’s a need to not only check whether stack contains a bubbling up exception, but also to get access to the exception itself.

For example, in Open Telemetry library ExceptionProcessor uses Marshal.GetExceptionPointers to check whether exception is thrown withing the scope of activity.

However, if I don’t want `OperationCancelledException’ to be treated as an error, I need to have access to the instance of the exception thrown. Currently, there’s no such possibility.

API Proposal

Well, I am not familiar with the intricacies of CLR, and whether such API is conceptually possible, but something like adding property to AppDomain could work:

public sealed class AppDomain
{
    public Exception? CurrentException {get;}
}

throw should set this property (overwriting previous value), catch with matching exception should set it to null.

API Usage

try
{
    throw new Exception();
}
finally
{
    // Here it is not null;
      Console.WriteLine(AppDomain.CurrentDomain.CurrentException is not null);
}
try
{
    throw new Exception();
}
catch
{
    // Here it is null;
   Console.WriteLine(AppDomain.CurrentDomain.CurrentException is null);
}
finally
{
    // Here it is null;
   Console.WriteLine(AppDomain.CurrentDomain.CurrentException is null);
}
try
{
    throw new Exception();
}
catch
{
    // Here it is null;
   Console.WriteLine(AppDomain.CurrentDomain.CurrentException is null);

   throw;
}
finally
{
    // Here it is not null;
   Console.WriteLine(AppDomain.CurrentDomain.CurrentException is not null);
}

Not sure whether value should propagate with async.

Task CheckMe() => Task.Run(() => Console.WriteLine(AppDomain.CurrentDomain.CurrentException is not null));
try
{
    throw new Exception();
}
finally
{
    // Here it is not null;
    Console.WriteLine(AppDomain.CurrentDomain.CurrentException is not null);

    // will it output true or false? 
    await CheckMe();

    // Here it is not null;
    Console.WriteLine(AppDomain.CurrentDomain.CurrentException is not null);
}

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

You are storing the handle in a thread-static local variable. The GCHandle will leak if the exception is thrown and caught, and then the thread exits.

@jkotas , despite it allocation with weak type?!

Yes, the weak type for GCHandle means that the target is referenced weekly. It does change that the GCHandle itself must be explicitly freed.

You want to use WeakReference<T> here. WeakReference is a GCHandle with finalizer that cleans it up when no longer used.

Could Open Telemetry achieve the same result using a first-chance exception handler? Whenever an exception is thrown, it could be stored in a thread/task -local field. The ExceptionProcessor could then access the most recent exception using the field and determine whether that exception is bubbling up using fnGetExceptionPointers.