{"record":{"id":"32dc37e7ebccf9c8","repo":"dotnet/efcore","slug":"translatecatchblock-unnamed-parameter-as-catch-va","errorCode":null,"errorMessage":"TranslateCatchBlock: unnamed parameter as catch variable","messagePattern":"TranslateCatchBlock: unnamed parameter as catch variable","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":792,"sourceCode":"        var translatedBody = Translate(catchBlock.Body) switch\n        {\n            BlockSyntax b => b,\n            StatementSyntax s => Block(s),\n            ExpressionSyntax e => Block(ExpressionStatement(e)),\n            _ => throw new ArgumentOutOfRangeException()\n        };\n\n        var catchDeclaration = noType\n            ? null\n            : CatchDeclaration(Generate(catchBlock.Test));\n\n        if (catchBlock.Variable is not null)\n        {\n            Check.DebugAssert(catchDeclaration is not null);\n\n            if (catchBlock.Variable.Name is null)\n            {\n                throw new NotSupportedException(\"TranslateCatchBlock: unnamed parameter as catch variable\");\n            }\n\n            catchDeclaration = catchDeclaration.WithIdentifier(Identifier(catchBlock.Variable.Name));\n        }\n\n        return CatchClause(\n            catchDeclaration,\n            catchBlock.Filter is null ? null : CatchFilterClause(Translate<ExpressionSyntax>(catchBlock.Filter)),\n            translatedBody);\n    }\n\n    /// <inheritdoc />\n    protected override Expression VisitConditional(ConditionalExpression conditional)\n    {\n        Result = TranslateConditional(conditional, lowerableAssignmentVariable: null);\n\n        return conditional;\n    }","sourceCodeStart":774,"sourceCodeEnd":810,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L774-L810","documentation":"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.","triggerScenarios":"Building a TryExpression with a CatchBlock whose Test type is set but the variable has no name.","commonSituations":"Manual CatchBlock construction via Expression.Catch(type, body) (which creates an unnamed variable) instead of Expression.Catch(variable, body).","solutions":["Create a named ParameterExpression for the catch variable: Expression.Catch(Expression.Parameter(typeof(Exception), \"ex\"), body)."],"exampleFix":"// before\nExpression.Catch(typeof(Exception), body)\n// after\nExpression.Catch(Expression.Parameter(typeof(Exception), \"ex\"), body)","handlingStrategy":"validation","validationCode":"// Ensure catch variables have names before translation\nforeach (var cb in AllCatchBlocks(expr))\n    if (cb.Variable is not null && string.IsNullOrEmpty(cb.Variable.Name))\n        throw new InvalidOperationException(\n            \"CatchBlock variable has no name; use a named ParameterExpression.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always construct CatchBlock with a named ParameterExpression: Expression.Catch(Expression.Parameter(type, \"ex\"), body).","Avoid the Expression.Catch(Type, body) overload when the catch body references the exception object."],"tags":["efcore","expression-trees","try-catch","parameterexpression"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}