Please add to C# language the "throws" keyword to mark methods, properties or constructors that may throw errors. Also, please add "try?", "try!" that will provide simple and fast way to handle errors. #6332
Replies: 3 comments 20 replies
-
See: #429 |
Beta Was this translation helpful? Give feedback.
-
catch ( Exception ex) { // specifically Exception, which is what this would have to be implemented as Any given catch should only specifically handle those things it can guarantee it can return a safe state from - eg: reading from a database you can handle catch sql, network, and serialization errors safely, as that code generally knows what to do if it can't get the thing it asked for. |
Beta Was this translation helpful? Give feedback.
-
Exceptions are extremely frustrating to deal with, and not having a way to enforce a try/catch around a method is even more so. It's far from the first time I'm frustrated, but right now I'm writing a UI in Avalonia, where letting exceptions bubble up through command bindings means the exception just disappears. No logs or error message dialog is terrible for users, so I have to put a try/catch everywhere around sensitive methods. But even if I know which methods are sensitive, C# gives me no tool to enforce that those exceptions are handled. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This approach is used by Swift. And it's wonderful. I changed/adjusted/extended their syntax a little.
Method declaration might look like this:
Constructor declaration might look like this:
Property declaration might look like this:
Main way to handle exception in method/property/constructor marked with “throws” keyword:
Alternatively to the “try… catch” blocks full declaration we can use it’s shortcut:
try! MethodsThatThrowsError(...); // method/property/constructor returns value or Throws RunTime exception if method throws error. Should be used if developer is sure that method that was marked with "throws" keyword will never throw any errors. "Try...Catch" construction is not required for the methods marked with "throws" key word if they were invoked with "try!"
Concepts:
Beta Was this translation helpful? Give feedback.
All reactions