-
Notifications
You must be signed in to change notification settings - Fork 214
Explicit Exception rethrowing #3094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Please take a look at #984, which is a proposal to add support for checked exceptions to Dart. It was closed as 'state-rejected', which means that the discussion is unlikely to be reopened, but at least you can see what various people said about this topic. |
All code can throw an error. Errors can be thrown for reasons not predicted by the code ( You shouldn't be catching errors. They are mistakes, and it's OK if they crash the program, because there is no telling which state the program is in after getting rudely interrupted in the middle of something. The throws that you should catch are exceptions, which usually implement That's basically Java's checked exceptions. Dart doesn't do that, for a number of reasons. A second reason is that it affects function subtyping. Dart has first-class functions as well as overriding (Java has overriding, so it's not a new issue). If a method declares as For Dart, first class function types would also need to carry the exceptions. int Function(String) parse = int.parse; if ( All in all, the lines are not so clear-cut, and any required overhead is going to be annoying in the cases where it's not needed, and the caller knows that for purely semantic reasons that cannot be expressed in the type system. I agree with @eernstg that this is unlikely to get a new response today. If anything, with nullable types and record return types and patterns, there are now more ways to report an exception than just throwing an |
Thank you both for your considerate responses. What's been said makes a lot of sense. I actually think Exceptions should not be used for usual runtime error handling. I don't mean an error in the sense you have described above but rather modelling of bad conditions i.e. handling invalid data at runtime. I much prefer to use error union return types or nullables to communicate and handle these cases. Unfortunately, the Dart docs explicitly recommend Exceptions be used for this purpose. In my own code I can simply not use exceptions and instead use error unions or nullables. Unfortunately code that I depend on does use Exceptions for this. And many more will take this path that is being recommended. It seems to be that Dart Errors (as you describe above) are the only valid case for exceptions. And that Dart Exceptions that are not of this class have no practical use, perhaps harming the language. At the very least, other error handling methods such as nullables or error unions should be recommended by the docs and use of Exceptions discouraged. Forgive me but the error handling story in Dart is very confusing and disappointing. |
Uh oh!
There was an error while loading. Please reload this page.
Currently is is possible to call functions without knowing if they
throw
. This leads to a sub-optimal developer experience and can easily result in a poor experience for the end application user.Especially since there is no indication in function signatures that an exception may be thrown or not; I have to read/search the implementation of every function that I depend on, liberally wrap swathes of code in
try
blocks or take the path of least resistance and just not handle errors until they later surface as a problem in production.I am proposing that exception handling be mandatory when calling functions that throw i.e. making the current implicit behavior explicit.
A basic example:
Remedy the compile error by explicitly catching and rethrowing
We could also consider a shorthand syntax, either by introducing a new keyword or maybe by overloading
rethrow
:I believe this would be a better developer experience, improving readability, prompting users to think more carefully about what error conditions may arise in their programs and ultimately result in higher quality code being written in Dart.
I suspect that because errors/exceptions are not encoded by the type system, that this would unfortunately be non-trivial to implement. Apologies if I am making incorrect assumptions. I am a Dart noob and not a PL nor compiler expert.
The text was updated successfully, but these errors were encountered: