Skip to content

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

Closed
EhsanAramide opened this issue Oct 18, 2021 · 5 comments
Closed

Analyzer and Error, Exception detection #47493

EhsanAramide opened this issue Oct 18, 2021 · 5 comments
Labels
legacy-area-analyzer Use area-devexp instead. type-enhancement A request for a change that isn't a bug

Comments

@EhsanAramide
Copy link

EhsanAramide commented Oct 18, 2021

In the current version which I use (dart SDK version 2.14.2), the analyzer can't detect which function and operation can throw exceptions
Some examples for clarification:

void isMetal(int z) {
  if (z == 1) {
    throw IsNotMetal('This is Hydrogen => H');
  }
  print('Yes it is');
}

In the above situation, analyzer doesn't provide an error which the isMetal method throws an IsNotMetal. Now analyzer can provide some way to fix code for example put out invocation in try/catch block which error type/s is same as thrown exception type in the function.

isMetal(z); // currently function will invoke and throws IsNotMetal

// after implementing this feature
// analyzer: the isMetal function throws IsNotMetal error
// analyzer_code_fix: wrrap with try/catch
isMetal(z); 

// and then if the code fix applied:
try {
  isMetal(z);
} catch(e) {
  // write your error handler here
}
@a-siva a-siva added legacy-area-analyzer Use area-devexp instead. type-enhancement A request for a change that isn't a bug labels Oct 18, 2021
@EhsanAramide
Copy link
Author

Another thing that I remember is we can make difference between passing constant values and non-constant values.
For example in the above example on z == 1 will throw exception so:

// analyzer warning: the isMetal function throws IsNotMetal
isMetal(1);

// There is no problem about z == 2 and analyzer doesn't make warning.
isMetal(2);

@srawlins
Copy link
Member

Thanks for the request. Can you be more specific about what you hope from the analyzer here?

analyzer doesn't provide an error which the isMetal method throws an IsNotMetal

There is no error. It is valid for code to throw exceptions.

analyzer can provide some way to fix code for example put out invocation in try/catch block which error type/s is same as thrown exception type in the function.

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.

Another thing that I remember is we can make difference between passing constant values and non-constant values.

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 package:test's fail method.

@bwilkerson
Copy link
Member

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?

@EhsanAramide
Copy link
Author

Thanks for your responses! 🙏🏾
As @bwilkerson discussed, I mean something in java for example the equivalent in java would be:

public void isMetal(int z) throws IsNotMetal {
  if (z == 1) {
    throw IsNotMetal('This is hydrogen => H');
  }
  doSomething(z);
}

and when we invoke isMetal we get a warning which this method throws an exception and wrap it with try/catch.
I mean this, is it unclear still?

@bwilkerson
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants