Skip to content

Commit 3f21053

Browse files
committed
d updated markdown snippets
1 parent 72610e6 commit 3f21053

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [Examples](#examples)
1616
* [Approved File Artifacts](#approved-file-artifacts)
1717
* [More Info](#more-info)
18+
* [No Checked Exceptions Philosophy](#no-checked-exceptions-philosophy)
1819
* [LICENSE](#license)
1920
* [Developer notes](#developer-notes)<!-- endToc -->
2021

approvaltests/docs/explanations/NoCheckedExceptions.md

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
<!-- toc -->
66
## Contents
7-
8-
* [Paintables](#paintables)
9-
* [Why](#why)
10-
* [How To](#how-to)
11-
* [create an animated gif](#create-an-animated-gif)<!-- endToc -->
7+
8+
* [Checked Exceptions Were a Mistake](#checked-exceptions-were-a-mistake)
9+
* [Mechanics](#mechanics)
10+
* [ObjectUtils.throwAsError](#objectutilsthrowaserror)
11+
* [ObjectUtils.throwAsError(() -> methodThatThrowsException(parameters))](#objectutilsthrowaserror---methodthatthrowsexceptionparameters)
12+
* [throw ObjectUtils.throwAsError(myCheckedException)](#throw-objectutilsthrowaserrormycheckedexception)<!-- endToc -->
1213

1314
## Checked Exceptions Were a Mistake
1415
Java was the first (and last) language to experiment with checked exceptions. The fact that no other language has adopted this "feature" should be strong enough evidence that it was a mistake.
@@ -27,15 +28,55 @@ If you have a single line that throws a checked exception, you can easily wrap i
2728
#### throw ObjectUtils.throwAsError(myCheckedException)
2829
If you want to rethrow from a try/catch block this method will rethrow what is passed-in wrapped in an unchecked exception (if needed). This method also states that it returns an unchecked exception allowing you to pretend to throw it, so you can easily compile code without having to add unnecessary return statements. For example:
2930

30-
snippet: throw_as_error_does_not_compile
31+
<!-- snippet: throw_as_error_does_not_compile -->
32+
<a id='snippet-throw_as_error_does_not_compile'></a>
33+
```java
34+
try
35+
{
36+
return methodThatThrowsCheckedException();
37+
}
38+
catch (Exception e)
39+
{
40+
ObjectUtils.throwAsError(e); // Does not compile. Missing "return" statement
41+
}
42+
```
43+
<sup><a href='/approvaltests-util-tests/src/test/java/com/spun/util/ThrowAsErrorExamples.java#L15-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-throw_as_error_does_not_compile' title='Start of snippet'>anchor</a></sup>
44+
<!-- endSnippet -->
3145

3246
Even though this would run, it won't compile. So instead you have to write the code like:
3347

34-
snippet: throw_as_error_with_return
48+
<!-- snippet: throw_as_error_with_return -->
49+
<a id='snippet-throw_as_error_with_return'></a>
50+
```java
51+
try
52+
{
53+
return methodThatThrowsCheckedException();
54+
}
55+
catch (Exception e)
56+
{
57+
ObjectUtils.throwAsError(e);
58+
return null; // This is never reached because the line above always throws an exception
59+
}
60+
```
61+
<sup><a href='/approvaltests-util-tests/src/test/java/com/spun/util/ThrowAsErrorExamples.java#L29-L39' title='Snippet source file'>snippet source</a> | <a href='#snippet-throw_as_error_with_return' title='Start of snippet'>anchor</a></sup>
62+
<!-- endSnippet -->
3563

3664
Since this return statement is never reached, instead we write it as throwing an exception returned from the method call.
3765

38-
snippet: throw_as_error_with_throw
66+
<!-- snippet: throw_as_error_with_throw -->
67+
<a id='snippet-throw_as_error_with_throw'></a>
68+
```java
69+
try
70+
{
71+
return methodThatThrowsCheckedException();
72+
}
73+
catch (Exception e)
74+
{
75+
throw ObjectUtils.throwAsError(e);
76+
}
77+
```
78+
<sup><a href='/approvaltests-util-tests/src/test/java/com/spun/util/ThrowAsErrorExamples.java#L43-L52' title='Snippet source file'>snippet source</a> | <a href='#snippet-throw_as_error_with_throw' title='Start of snippet'>anchor</a></sup>
79+
<!-- endSnippet -->
3980

4081

4182

0 commit comments

Comments
 (0)