dotnet/efcore · error · NotSupportedException

TranslateCatchBlock: unnamed parameter as catch variable

Error message

TranslateCatchBlock: unnamed parameter as catch variable

What it means

Thrown in TranslateCatchBlock when catchBlock.Variable is not null but catchBlock.Variable.Name is null. A catch clause needs a named variable to emit 'catch (Exception ex)'; an unnamed catch variable cannot be rendered. NotSupportedException. Part of EF Core's LINQ-to-C# syntax translation.

Source

Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:792

        var translatedBody = Translate(catchBlock.Body) switch
        {
            BlockSyntax b => b,
            StatementSyntax s => Block(s),
            ExpressionSyntax e => Block(ExpressionStatement(e)),
            _ => throw new ArgumentOutOfRangeException()
        };

        var catchDeclaration = noType
            ? null
            : CatchDeclaration(Generate(catchBlock.Test));

        if (catchBlock.Variable is not null)
        {
            Check.DebugAssert(catchDeclaration is not null);

            if (catchBlock.Variable.Name is null)
            {
                throw new NotSupportedException("TranslateCatchBlock: unnamed parameter as catch variable");
            }

            catchDeclaration = catchDeclaration.WithIdentifier(Identifier(catchBlock.Variable.Name));
        }

        return CatchClause(
            catchDeclaration,
            catchBlock.Filter is null ? null : CatchFilterClause(Translate<ExpressionSyntax>(catchBlock.Filter)),
            translatedBody);
    }

    /// <inheritdoc />
    protected override Expression VisitConditional(ConditionalExpression conditional)
    {
        Result = TranslateConditional(conditional, lowerableAssignmentVariable: null);

        return conditional;
    }

View on GitHub (pinned to dbf9771522)

Solutions

  1. Create a named ParameterExpression for the catch variable: Expression.Catch(Expression.Parameter(typeof(Exception), "ex"), body).

Example fix

// before
Expression.Catch(typeof(Exception), body)
// after
Expression.Catch(Expression.Parameter(typeof(Exception), "ex"), body)
Defensive patterns

Strategy: validation

Validate before calling

// Ensure catch variables have names before translation
foreach (var cb in AllCatchBlocks(expr))
    if (cb.Variable is not null && string.IsNullOrEmpty(cb.Variable.Name))
        throw new InvalidOperationException(
            "CatchBlock variable has no name; use a named ParameterExpression.");

Prevention

When it happens

Trigger: Building a TryExpression with a CatchBlock whose Test type is set but the variable has no name.

Common situations: Manual CatchBlock construction via Expression.Catch(type, body) (which creates an unnamed variable) instead of Expression.Catch(variable, body).

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/32dc37e7ebccf9c8. Report an issue: GitHub.