|
4 | 4 |
|
5 | 5 | import java.io.IOException; |
6 | 6 |
|
7 | | -public class ThrowAsErrorExamples { |
8 | | - @Test |
9 | | - void examples() { |
10 | | - |
| 7 | +public class ThrowAsErrorExamples |
| 8 | +{ |
| 9 | + @Test |
| 10 | + void examples() |
| 11 | + { |
| 12 | + } |
| 13 | + Integer whyReturnIsNeeded() |
| 14 | + { |
| 15 | + // begin-snippet: throw_as_error_does_not_compile |
| 16 | + try |
| 17 | + { |
| 18 | + return methodThatThrowsCheckedException(); |
11 | 19 | } |
12 | | - Integer whyReturnIsNeeded() { |
13 | | - // begin-snippet: throw_as_error_does_not_compile |
14 | | - try { |
15 | | - return methodThatThrowsCheckedException(); |
16 | | - } catch (Exception e) { |
17 | | - ObjectUtils.throwAsError(e); // Does not compile. Missing "return" statement |
18 | | - } |
19 | | - // end-snippet |
20 | | - return 0; |
| 20 | + catch (Exception e) |
| 21 | + { |
| 22 | + ObjectUtils.throwAsError(e); // Does not compile. Missing "return" statement |
21 | 23 | } |
22 | | - Integer withReturn() { |
23 | | - // begin-snippet: throw_as_error_with_return |
24 | | - try { |
25 | | - return methodThatThrowsCheckedException(); |
26 | | - } catch (Exception e) { |
27 | | - ObjectUtils.throwAsError(e); |
28 | | - return null; // This is never reached because the line above always throws an exception |
29 | | - } |
30 | | - // end-snippet |
| 24 | + // end-snippet |
| 25 | + return 0; |
| 26 | + } |
| 27 | + Integer withReturn() |
| 28 | + { |
| 29 | + // begin-snippet: throw_as_error_with_return |
| 30 | + try |
| 31 | + { |
| 32 | + return methodThatThrowsCheckedException(); |
31 | 33 | } |
32 | | - Integer withThrow() { |
33 | | - // begin-snippet: throw_as_error_with_throw |
34 | | - try { |
35 | | - return methodThatThrowsCheckedException(); |
36 | | - } catch (Exception e) { |
37 | | - throw ObjectUtils.throwAsError(e); |
38 | | - } |
39 | | - // end-snippet |
| 34 | + catch (Exception e) |
| 35 | + { |
| 36 | + ObjectUtils.throwAsError(e); |
| 37 | + return null; // This is never reached because the line above always throws an exception |
40 | 38 | } |
41 | | - |
42 | | - private Integer methodThatThrowsCheckedException() throws IOException { |
43 | | - return 1; |
| 39 | + // end-snippet |
| 40 | + } |
| 41 | + Integer withThrow() |
| 42 | + { |
| 43 | + // begin-snippet: throw_as_error_with_throw |
| 44 | + try |
| 45 | + { |
| 46 | + return methodThatThrowsCheckedException(); |
| 47 | + } |
| 48 | + catch (Exception e) |
| 49 | + { |
| 50 | + throw ObjectUtils.throwAsError(e); |
44 | 51 | } |
| 52 | + // end-snippet |
| 53 | + } |
| 54 | + private Integer methodThatThrowsCheckedException() throws IOException |
| 55 | + { |
| 56 | + return 1; |
| 57 | + } |
45 | 58 | } |
0 commit comments