Cysharp/UniTask · error · ArgumentNullException

exception

Error message

exception

What it means

Thrown by WhenEachResult<T>(Exception) constructor when the exception argument is null. A WhenEachResult is either a success (with a result value) or a failure (with an exception); constructing a failure result with a null exception is logically contradictory. The guard enforces that a faulted result always carries a real exception.

Source

Thrown at src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.WhenEach.cs:41

    {
        public T Result { get; }
        public Exception Exception { get; }

        //[MemberNotNullWhen(false, nameof(Exception))]
        public bool IsCompletedSuccessfully => Exception == null;

        //[MemberNotNullWhen(true, nameof(Exception))]
        public bool IsFaulted => Exception != null;

        public WhenEachResult(T result)
        {
            this.Result = result;
            this.Exception = null;
        }

        public WhenEachResult(Exception exception)
        {
            if (exception == null) throw new ArgumentNullException(nameof(exception));
            this.Result = default;
            this.Exception = exception;
        }

        public void TryThrow()
        {
            if (IsFaulted)
            {
                ExceptionDispatchInfo.Capture(Exception).Throw();
            }
        }

        public T GetResult()
        {
            if (IsFaulted)
            {
                ExceptionDispatchInfo.Capture(Exception).Throw();
            }

View on GitHub (pinned to ceac8d6946)

Solutions

  1. Null-check the exception before constructing a faulted WhenEachResult
  2. If the exception could be null, provide a fallback exception (e.g., new InvalidOperationException("Unknown error"))
  3. Ensure task sources that fault always provide a non-null exception

Example fix

// before
var result = new WhenEachResult<T>(nullableException); // throws if null

// after
var result = new WhenEachResult<T>(
    nullableException ?? new InvalidOperationException("Task faulted with null exception"));
Defensive patterns

Strategy: validation

Validate before calling

if (exception == null)
{
    exception = new InvalidOperationException("Task faulted with null exception");
}
var result = new WhenEachResult<T>(exception);

Type guard

static Exception EnsureNonNull(Exception ex) =>
    ex ?? new InvalidOperationException("Unknown error");

Prevention

When it happens

Trigger: Calling new WhenEachResult<T>((Exception)null) or new WhenEachResult<T>(someNullableException) where the nullable value is null. This typically happens inside WhenEach's internal completion handling rather than in user code directly.

Common situations: WhenEach enumerating tasks where a task faults with a null exception (unusual but possible via custom task sources). User code constructing WhenEachResult manually with an unchecked null exception variable.

Related errors


AI-assisted analysis of Cysharp/UniTask@ceac8d6946 (2026-08-13). Data as JSON: /api/errors/9d6d0443816d75b5. Report an issue: GitHub.