Skip to content

Commit 3a4db98

Browse files
authored
Update coding-conventions.asciidoc
1 parent dce05e7 commit 3a4db98

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

documentation/coding-conventions.asciidoc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ try {
184184
readData(in);
185185
in.close();
186186
} catch (IOException e) {
187-
throw new RuntimeIoException(e, IoMode.READ);
187+
throw new IllegalStateException("Failed to read data.", e);
188188
}
189189
----
190190

@@ -194,10 +194,18 @@ The code above is wrong as in case of an `IOException` the `InputStream` is not
194194
try (InputStream in = new FileInputStream(file)) {
195195
readData(in);
196196
} catch (IOException e) {
197-
throw new RuntimeIoException(e, IoMode.READ);
197+
throw new IllegalStateException("Failed to read data.", e);
198198
}
199199
----
200200

201+
=== Catching and handling Exceptions
202+
When catching exceptions always ensure the following:
203+
204+
* Never call `printStackTrace()` method on an exception
205+
* Either log or wrap and re-throw the entire catched exception. Be aware that the cause(s) of an exception is very valuable information. If you loose such information by improper exception-handling you may be unable to properly analyse production problems what can cause severe issues.
206+
** If you wrap and re-throw an exception ensure that the catched exception is passed as cause to the newly created and thrown exception.
207+
** If you log an exception ensure that the entire exception is passed as argument to the logger (and not only the result of `getMessage()` or `toString()` on the exception).
208+
* See link:guide-exceptions.asciidoc#handling-exceptions[exception handling]
201209

202210
=== Lambdas and Streams
203211
With Java8 you have cool new features like lambdas and monads like (`Stream`, `CompletableFuture`, `Optional`, etc.).

0 commit comments

Comments
 (0)