[Proposal]: Try Expressions #9414
Unanswered
ahives
asked this question in
Language Ideas
Replies: 3 comments 2 replies
-
The question here is, what happens in that "switch case" where you have |
Beta Was this translation helpful? Give feedback.
2 replies
-
I very like it var res = GetRes() catch GetAlternative(); var res = GetRes() catch Ex when ... => GetAlternative(); var res = GetRes() catch Ex => GetAlternative(); Result SomeExHappened(Exception ex) => ex switch {
Ex when ... => GetAlternative()
//raise up the exception when here
};
var res = GetRes() catch SomeExHappened;
or
var res = GetRes() catch myexvar => SomeExHappened(myexvar); var res = GetRes() catch {
Ex when ... => ...
Ex2 when ... => ...
//raise up the exception when here
}; var res = GetRes()
catch GetAlternative()
catch GetAlternative2(); var res = GetRes() catch GetAlternative() finaly Smth(); //same
var res = GetRes() catch GetAlternative() finaly Smth() catch GetAlternative2() finaly Smth2();
var res = ( GetRes() catch GetAlternative() finaly Smth() ) catch GetAlternative2() finaly Smth2(); var res = GetRes() finaly Do(); |
Beta Was this translation helpful? Give feedback.
0 replies
-
There was talk at one point about integrating exceptions into pattern matching, something like var res = try GetValue() switch
{
_ => ... // Normal pattern matching
catch SomeException e => ...
catch SomeOtherException e => ...
catch e => ...
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Currently, if we want to handle multiple exception types in C# we must do the following:
The compiler will check for the target exception in the order in which the exception handlers were declared (i.e., SomeException, SomeOtherException, ..., etc.). However, there is one caveat, that is, if you declare a handler that matches on
System.Exception
then it acts to shortcircuit subsequent handlers. This is exceptable since the compiler will error on this becauseSystem.Exception
essentially hides all other handlers from executing since they derive fromSystem.Exception
. This works but it suffers from a couple problems:catch (...) { ... }
syntax for each handlerMotivation
C# 8 introduced switch expressions which presented a natural evolution of the switch syntax away from the decades old if-else style that its the try-catch pattern to something more expressive that is step with other modern languages. In C# 7 pattern matching was introduced for switch statements to allow for matching on object types. The language is already going in the direction of enabling pattern matching in branch logic. Try expressions are the next logical step toward enabling application developers to create expressive patterns to handle exceptions.
Proposal
The purpose of this proposal is to introduce functionality in the C# programming language that would make handling exceptions:
catch(...) { ... }
logic for each exception handlerFor the reasons I propose the following syntax I am calling try expressions:
This proposed language feature is similar to the
switch
expression syntax. A few things to notice:System.Exception
. Instead, you could simply use an underscore, _, catch all expression to represent the catch all case.when
keyword like so,A working example looks something like this ,
The thinking here is that the handler could be a simple expression that could return a value or void. For more complex tasks there could be a code block such that you do something like this,
In the case that it is an expression that returns a value you should be able to use it like this,
In the functional programming the idea is to reduce the application from blowing up by all means possible. So, in some instances you don't want exceptions to bubble up the call chain. Instead, you want to return a monad that has error information so that the caller can deal with it more appropriately. This sort of pattern can already be done like so,
Notice a couple of things:
Let's reimagine the above code with what is being proposed using expression body syntax,
There is precedence for this expressive pattern both in C# and other languages like Elixir. For reference I provided a link to Elixir docs here to show how it is being used in other languages.
Drawbacks
I don't see any drawbacks since current applications will continue to work as they currently do upon upgrading to a version with try expressions released to the runtime. Also, this pattern is inline with work done in previous versions of the runtime including switch expressions, pattern matching, and expression body syntax.
Alternatives
As noted previously there are current ways to express error handling both conditionally and otherwise. However, it would be a bit strange to implement expression syntax in branch logic for switch statements and not do so for try-catch statements.
Beta Was this translation helpful? Give feedback.
All reactions