Add Checked Exception feature to C# #2937
-
There is no way to know what Exception the method will throw, we can only beg the mercy from the author to write the comment in their code! It is Weird! |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
The Trouble with Checked Exceptions - a conversation with Anders Hejlsberg. So, if you want checked exceptions in C#, what's your design proposal? How do you avoid the versioning and scaling issues articulated by Anders in the above interview? |
Beta Was this translation helpful? Give feedback.
-
And that was written long before Java attempted to go functional. Checked exceptions work exceptionally poorly (har har) in a functional world since they cannot be composed. You end up having to write |
Beta Was this translation helpful? Give feedback.
-
Maybe we need union type to do some help! |
Beta Was this translation helpful? Give feedback.
-
IMO, checked exception should be implemented like NRT static analysis rather than using This warnings then prompt us to catch all the uncaught exception, or suppress the warnings, or optionally crash the app programmatically.
On versioning: This approach infers the list of thrown exception instead of requiring us to explicitly annotate methods with On scaling: Since we only care about uncaught exception thrown by "top level" methods, compiler can first build a graph of calls originating from these methods, then the compiler build graph of throws only for codes reached by the graph of calls. Both graphs can be rebuilt incrementally. // Page_Load gets called by the runtime.
public async void Page_Load()
{
// warning: OperationCanceledException is uncaught.
// Catch all exceptions to avoid crash.
this.Title = await GetTitleAsync(CancellationToken.None);
}
// GetTitleAsync doesn't get called directly by runtime. It doesn't raise the warning.
public async Task<string> GetTitleAsync(CancellationToken cancellationToken)
{
...
} Or we can annotate our own code with the attribute: [DoesNotRecoverFromException]
public void Foo()
{
// warning: IOException is uncaught
// Catch all exceptions to avoid crash.
throw new IOException();
} |
Beta Was this translation helpful? Give feedback.
-
I'm curious. Have Microsoft (or anyone else) tried this kind of static analysis to try find uncaught exceptions in non trivial applications? Without annotations and tooling, my gut feeling is telling me that the nitpicking and false positives would ruin the experience and might be more of a hindrance than help. |
Beta Was this translation helpful? Give feedback.
-
They don't catch exceptions that not recoverable at the medium level. The specified failure of a type of task will have a general base type, and highlighted in the documentation. |
Beta Was this translation helpful? Give feedback.
-
Checked exceptions as implemented in Java won't work very well.
On the other hand, I have to admit that when Java checked exceptions do work, they are a pleasure to work with. If Microsoft ever decides to write a language for the next few decades, it should definitely let you express failed states explicitly and with sufficient granularity. |
Beta Was this translation helpful? Give feedback.
-
I'd be happy to see good DU support that would enable I noticed that OP didn't explicitly mention Java's checked exceptions, although they seem to be the prototypical implementation that people refer to. I'd be game to explore what checked exceptions might look like if implemented with functional programming in mind. I'd be happy to hear how other languages may have implemented them differently that void the pitfalls of the Java implementation. But unless we can come up with something that "moves the art forward", it's better to not try to add it to the language. |
Beta Was this translation helpful? Give feedback.
-
Here is more detail proposal #2052 and it fits with what I wanted myself to propose =) |
Beta Was this translation helpful? Give feedback.
The Trouble with Checked Exceptions - a conversation with Anders Hejlsberg.
So, if you want checked exceptions in C#, what's your design proposal? How do you avoid the versioning and scaling issues articulated by Anders in the above interview?