diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/SwitchExpressions.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/SwitchExpressions.cs index a7f38a4d4..b4ecedbd4 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/SwitchExpressions.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/SwitchExpressions.cs @@ -244,6 +244,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty }).ToValidated(); } + public static void ThrowDifferentExceptions(int i) + { + throw i switch { + 0 => (Exception)new ArgumentException("Invalid argument"), + 1 => new InvalidOperationException("Invalid operation"), + _ => new NotSupportedException(), + }; + } + public static int SwitchOnStringImplicitDefault(string s) { return s switch { diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index 435881438..24c817873 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -1272,7 +1272,8 @@ namespace ICSharpCode.Decompiler.CSharp protected internal override TranslatedExpression VisitThrow(Throw inst, TranslationContext context) { - return new ThrowExpression(Translate(inst.Argument)) + var ex = Translate(inst.Argument, typeHint: compilation.FindType(KnownTypeCode.Exception)); + return new ThrowExpression(ex) .WithILInstruction(inst) .WithRR(new ThrowResolveResult()); } diff --git a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs index 1314702d1..3385a2c2a 100644 --- a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs @@ -434,7 +434,8 @@ namespace ICSharpCode.Decompiler.CSharp protected internal override TranslatedStatement VisitThrow(Throw inst) { - return new ThrowStatement(exprBuilder.Translate(inst.Argument)).WithILInstruction(inst); + var ex = exprBuilder.Translate(inst.Argument, typeHint: typeSystem.FindType(KnownTypeCode.Exception)); + return new ThrowStatement(ex).WithILInstruction(inst); } protected internal override TranslatedStatement VisitRethrow(Rethrow inst)