Proposal: Exception coalescing operator (?!:) #8584
Replies: 3 comments 4 replies
-
Exceptions are meant to be exceptional. You should not be catching them where they happen, but further up the stack where you can report a failure condition to the user. |
Beta Was this translation helpful? Give feedback.
-
We should not waste time on exception handling. It's an awful paradigm that we should move away from. What we should do however is make it easier to handle error states by improving pattern matching and optional types |
Beta Was this translation helpful? Give feedback.
-
IMO it should be pretty rare to want to catch an exception and ignore it, to the extent that I don't think the language should make it easier. It should stand out in the code and require a deal of friction to force the developer intent. |
Beta Was this translation helpful? Give feedback.
-
In the spirit of the extremely useful null coalescing operator, it could be a really useful feature to handle expected exceptions inline when evaluating expressions.
Pattern:
Where the expression can be a property being read or a non-void method.
Consider the following
Example
class:Then a few examples of the operator's usage:
In many scenarios we are aware and expect exceptions to be thrown from a single expression. Be it a method's return value being assigned to a variable and throwing an exception or a a property getter doing the same, dealing with expected exceptions inline have the advantages of improved code readability, as we won't have to create a try..catch block for a single line of code. It will also prevent creating aesthetically (readable) appealing try..catch blocks by wrapping more code then we expect to be throwing an exception (such as the lines around it, or the full-body of the current scope), which could cause the accidental catch of an unexpected exception at a different line and become a debugging hassle.
Beta Was this translation helpful? Give feedback.
All reactions