It is sometimes necessary to try/catch exceptions inside the if condition, but not the body that follows. In C#, this is really rather cumbersome, requiring locals and code that isn't entirely obvious, at a glance, as to its operation.It is also sometimes necessary to write switch statements, which, unlike if, have more than two possible outcomes, although in C# this requires the use of a fairly unpopular syntax.Here's an idea for combining these.Merging switch with try/catchFirst, we change the fundamental syntax of switch to be close to that fairly popular replacement, but without the extra curlies:switch (condition)    case 1 { }    case 2 { }    default { }Next, we make it possible to catch exceptions in the condition. Under the hood, this inserts an appropriate try/catch block only when at least one catch is present:switch (condition)    catch FileNotFoundException e { } // use e    catch ArgumentException { }    catch { }    case 1 { }    case 2 { }    default { }Now we make it easier to chain several such statements akin to if/else chaining, by moving default one level up, and renaming it to else:switch (condition1)    catch FileNotFoundException { }    case 1 { }    case 2 { }else switch (condition2)    case abc { }    case def { }else    { }And so we have a switch merged with try/catch!Combining with 'if'Both switch and if now have else clauses, so why not allow you to mix them as you please:if (bool-condition1)    { }else if (bool-condition2)    { }else switch (string-expr)    catch FileNotFoundException { }    case thing { }else    { }(thanks ach_l!)Further improvementsSwitching on a list or range of values has been proposed and can be easily incorporated:switch (int-expression)    case 1 { }    case 2, 5 { }    case 10..50 { }else switch (string-expression)    case abc { }    case def { }else if (boolean-condition3)    { }else    { }In this case it could be really convenient to be able to reference the value in the condition clause. This can be easily incorporated by allowing one to declare a single variable in this clause the same way C#'s using allows:switch (var z = int-expression)    case 2, 5 { return z == 2 ? two : five; }    case 10..50 { return (z * 10).ToString(); }BenefitsAmong other benefits, one can now express a common error-handling scenario concisely, and yet not WTFy:switch (bool-condition)    catch FileNotFoundException e { }    case true { }    case false { }Would this solve any real-life coding difficulties you have encountered? Are there any tweaks you would apply to this?
A combined if/switch statement syntax with exception handling for a C#-inspired language
language design;language features;syntax
Personally I would leave if as evaluating a boolean argument with a true path and optional false else path, whereas switch could evaluate other objects for equality, membership of a set, etc. It may not be a good reason, but this is the behaviour of if in many other languages. To change that definition I think we would risk confusing new programmers. But I could definately see this syntax of catch blocks being handy, and the ability to chain together ifs and switches might also have a use case.Personally I tend to like a style where the name of the argument is defined by the block, Additionally, if the value of a condition is being evaluated, we could optionally provide it to the case blocks as well!Perhaps this example may shed light on the scope of what I'm thinking building on this:if(<boolean>condition1)    catch Exception { e -> /* exception referenced by e */ }     { /*true*/ } else switch(<any>condition2)    catch FileNotFoundException { /* I don't care about this exception*/ }   case 1 {  /* I don't need condition2's value */  }   case 2, 5, 10 .. 20 { v -> /* v is condition 2 value */ }    case <50 { x -> /* x is condition 2's value, this would only be evaluated if other branches are not */}else    { /* !condition1 and no case evaluated in condition 2 */ }