Skip to content
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

RethrowCaughtException should not be triggered when there is more general exception below #4367

Closed
matejdro opened this issue Dec 10, 2021 · 3 comments · Fixed by #4369
Closed

Comments

@matejdro
Copy link
Contributor

Expected Behavior of the rule

Following code should be compliant with RethrowCaughtException rule:

try {
    // Do something
} catch (e: CancellationException) {
    // Just throw cancellation
    throw e
} catch (e: Exception) {
    // Log everything except cancellation
    logException(e)
}

Context

In above case I want to log all exceptions except for the Cancellation exception (which I just want to propagate). That's why CancellationException has its own catch that gets activated before the general one, which is then never reached. This cause clause is not useless because it is used to filter more general cause clauses that come after it.

Above snippet is just a more cleaner way to write following code, which is compliant:

try {
    // Do something
} catch (e: Exception) {
    if (e is CancellationException) {
        throw e
    } else {
        logException(e)
    }
}
@BraisGabin
Copy link
Member

You are right. Do you want to give it a try and send a PR solving it?

@strkkk
Copy link
Contributor

strkkk commented Dec 10, 2021

@BraisGabin I would like to give it a try

@BraisGabin
Copy link
Member

Go for it! Let me know if you need any help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants