PEP 484 introduced type hints, at this time documenting exceptions was left to docstrings. I seek to suggest a reason this feature might be desirable along with how it might be used. Error handling in python does an excellent job of keeping the error-path out of the way when writing the normal flow of logic, however for larger code bases it is not always clear what exceptions may be caused by calling existing code. Since these cases are easily missed they may reach a higher level than intended ...
This is a discussion on Python’s forums about adding something akin to a throws keyword in python.
I’m not too familiar with C# (last used it like a decade ago), but I think the rules here would be pretty simple:
x? - if x is None or an Error, return from the function early, otherwise use the value and continue
x?.y - same as above, but with an attribute of x
x ?? y - instead of returning as in the first, use y as the default value
And maybe add an option to convert exceptions from a function to an Error value (maybe some_func?() to convert to error values? IDK, I haven’t thought through that part as much).
Hopefully that’s simple enough to be useful.
If I were proposing this, I’d limit it to optional chaining since that’s far more annoying to me currently.
I’m not too familiar with C# (last used it like a decade ago), but I think the rules here would be pretty simple:
And maybe add an option to convert exceptions from a function to an Error value (maybe
some_func?()
to convert to error values? IDK, I haven’t thought through that part as much).Hopefully that’s simple enough to be useful.
If I were proposing this, I’d limit it to optional chaining since that’s far more annoying to me currently.