|
| 1 | +import java.io.IOException; |
| 2 | +import java.util.Optional; |
| 3 | + |
| 4 | +class ErrorHandling { |
| 5 | + |
| 6 | + void handleErrorByThrowingIllegalArgumentException() { |
| 7 | + throw new IllegalArgumentException(); |
| 8 | + } |
| 9 | + |
| 10 | + void handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage(String message) { |
| 11 | + throw new IllegalArgumentException(message); |
| 12 | + } |
| 13 | + |
| 14 | + void handleErrorByThrowingAnyCheckedException() throws IOException { |
| 15 | + throw new IOException(); |
| 16 | + } |
| 17 | + |
| 18 | + void handleErrorByThrowingAnyCheckedExceptionWithDetailMessage(String message) throws IOException { |
| 19 | + throw new IOException(message); |
| 20 | + } |
| 21 | + |
| 22 | + void handleErrorByThrowingAnyUncheckedException() { |
| 23 | + throw new NullPointerException(); |
| 24 | + } |
| 25 | + |
| 26 | + void handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage(String message) { |
| 27 | + throw new NullPointerException(message); |
| 28 | + } |
| 29 | + |
| 30 | + void handleErrorByThrowingCustomCheckedException() throws CustomCheckedException { |
| 31 | + throw new CustomCheckedException(); |
| 32 | + } |
| 33 | + |
| 34 | + void handleErrorByThrowingCustomCheckedExceptionWithDetailMessage(String message) throws CustomCheckedException { |
| 35 | + throw new CustomCheckedException(message); |
| 36 | + } |
| 37 | + |
| 38 | + void handleErrorByThrowingCustomUncheckedException() { |
| 39 | + throw new CustomUncheckedException(); |
| 40 | + } |
| 41 | + |
| 42 | + void handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage(String message) { |
| 43 | + throw new CustomUncheckedException(message); |
| 44 | + } |
| 45 | + |
| 46 | + Optional<Integer> handleErrorByReturningOptionalInstance(String integer) { |
| 47 | + if (tryParseInt(integer)) { |
| 48 | + return Optional.of(Integer.parseInt(integer)); |
| 49 | + } else { |
| 50 | + return Optional.empty(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private boolean tryParseInt(String integer) { |
| 55 | + try { |
| 56 | + Integer.parseInt(integer); |
| 57 | + return true; |
| 58 | + } catch (NumberFormatException e) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments