Skip to content

Commit

Permalink
Latest docs on successful build 6665 auto-pushed to gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
copybara-service[bot] committed Sep 13, 2024
1 parent 2e4e80a commit b09da34
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions bugpattern/EmptyCatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,35 @@ To make changes, edit the @BugPattern annotation or the explanation in docs/bugp
-->


## The problem
The [Google Java Style Guide §6.2][style] states:

> It is very rarely correct to do nothing in response to a caught exception.
> (Typical responses are to log it, or if it is considered "impossible", rethrow
> it as an AssertionError.)
>
> When it truly is appropriate to take no action whatsoever in a catch block,
> the reason this is justified is explained in a comment.
When writing tests that expect an exception to be thrown, prefer using
[`Assert.assertThrows`][assertthrows] instead of writing a try-catch. That is,
prefer this:

```java
assertThrows(NoSuchElementException.class, () -> emptyStack.pop());
```

instead of this:

```java
try {
emptyStack.pop();
fail();
} catch (NoSuchElementException expected) {
}
```

[style]: https://google.github.io/styleguide/javaguide.html#s6.2-caught-exceptions

[assertthrows]: https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThrows(java.lang.Class,%20org.junit.function.ThrowingRunnable)

0 comments on commit b09da34

Please sign in to comment.