-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Analyzer and Error, Exception detection #47493
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
Another thing that I remember is we can make difference between passing constant values and non-constant values. // analyzer warning: the isMetal function throws IsNotMetal
isMetal(1);
// There is no problem about z == 2 and analyzer doesn't make warning.
isMetal(2); |
Thanks for the request. Can you be more specific about what you hope from the analyzer here?
There is no error. It is valid for code to throw exceptions.
This is an interesting assist the analyzer could do. @bwilkerson might have some great design insights here. But once again, it is valid to throw exceptions, so the analyzer cannot just report on a function which throws. But perhaps there is an idea here to provide an assist on either a function declaration, or a throw expression, to offer to wrap with a try/catch.
Analyzer does evaluate constant expressions (in many? most? cases), but does not do any further program execution (or data flow analysis) using expressions which are constant. We do not have plans to take on this task. And, in any case, it is perfectly legal to call a method which is guaranteed to throw an exception. It can also be intentional, such as calling |
I'm not sure I understand what you're requesting here. Are you asking for something like checked exceptions in Java (see #45884), or something else? |
Thanks for your responses! 🙏🏾 public void isMetal(int z) throws IsNotMetal {
if (z == 1) {
throw IsNotMetal('This is hydrogen => H');
}
doSomething(z);
} and when we invoke |
That's clear, thanks. Dart doesn't currently have any support for anything similar to checked exceptions, so the kind of checks that you're asking for aren't currently possible. Issue #45884 is a request to add such support, either to the language directly or as an annotation that users can opt into using. If support is added, then I expect that the behavior you're requesting would be included in that support, so I'm going to close this request as a duplicate of the other. |
In the current version which I use (
dart SDK version 2.14.2
), the analyzer can't detect which function and operation can throw exceptionsSome examples for clarification:
In the above situation, analyzer doesn't provide an error which the
isMetal
method throws anIsNotMetal
. Now analyzer can provide some way to fix code for example put out invocation intry/catch
block which error type/s is same as thrown exception type in the function.The text was updated successfully, but these errors were encountered: