Shorten example + some more comments per PR feedback - https://github.com/mmcgrana/gobyexample/pull/344#pullrequestreview-590513160
This commit is contained in:
parent
481091c61f
commit
47eed5ec17
@ -1,5 +1,5 @@
|
||||
// A `recover` means recovering from a `panic`, either from a "business" or "built-in" panic.
|
||||
// We want to recover if we want to handle a panic, stopping it from propagating upwards.
|
||||
// A `recover` means recovering from a `panic`,
|
||||
// stopping the panic from propagating upwards.
|
||||
|
||||
package main
|
||||
|
||||
@ -8,31 +8,15 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
recoverFromPanic(-1)
|
||||
|
||||
recoverFromBuiltInPanic(10)
|
||||
|
||||
fmt.Println()
|
||||
|
||||
recoverFromCustomPanic(-1)
|
||||
// We see it because we recovered from a panic.
|
||||
fmt.Printf("Finished without panicing.")
|
||||
}
|
||||
|
||||
func recoverFromBuiltInPanic(i int) {
|
||||
// defer is defined.
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Println("Recovered. Error:\n", r)
|
||||
}
|
||||
}()
|
||||
|
||||
var a [5]int
|
||||
fmt.Printf("Getting index %d"+
|
||||
" of array of len %d...\n", i, len(a))
|
||||
fmt.Printf("Item in index %d: %d", i, a[i])
|
||||
}
|
||||
|
||||
func recoverFromCustomPanic(i int) {
|
||||
// defer is defined.
|
||||
func recoverFromPanic(i int) {
|
||||
defer func() {
|
||||
// recover is always defined in a defer.
|
||||
if r := recover(); r != nil {
|
||||
fmt.Println("Recovered. Error:\n", r)
|
||||
}
|
||||
@ -45,5 +29,6 @@ func recoverFromCustomPanic(i int) {
|
||||
" non-negative numbers but received %d", i))
|
||||
}
|
||||
|
||||
// We won't see this because we paniced.
|
||||
fmt.Printf("Doing something with %d\n", i)
|
||||
}
|
||||
|
@ -1,2 +1,2 @@
|
||||
a098d6e6717560d0b67ae579b78e00a74cf8781a
|
||||
uVo4c0IE97q
|
||||
8e872b4ab1b23636c5718b557cac09a4c2f10e04
|
||||
3JB9O01LGko
|
||||
|
@ -1,13 +1,9 @@
|
||||
# Running this program will exit correctly,
|
||||
# even though panic was invoked in two methods.
|
||||
# The recover is responsible for recovering from panics.
|
||||
# even though panic was called.
|
||||
$ go run recover.go
|
||||
Getting index 10 of array of len 5...
|
||||
Recovered. Error:
|
||||
runtime error: index out of range [10] with length 5
|
||||
|
||||
About to process i=-1
|
||||
Recovered. Error:
|
||||
Accepting only non-negative numbers but received -1
|
||||
Finished without panicing.
|
||||
# Note that, in Go it is idiomatic
|
||||
# to use error-indicating return values wherever possible.
|
||||
|
108
public/recover
generated
108
public/recover
generated
@ -27,8 +27,8 @@
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>A <code>recover</code> means recovering from a <code>panic</code>, either from a “business” or “built-in” panic.\n
|
||||
We want to recover if we want to handle a panic, stopping it from propagating upwards.</p>
|
||||
<p>A <code>recover</code> means recovering from a <code>panic</code>,
|
||||
stopping the panic from propagating upwards.</p>
|
||||
|
||||
</td>
|
||||
<td class="code empty leading">
|
||||
@ -42,7 +42,7 @@ We want to recover if we want to handle a panic, stopping it from propagating up
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/uVo4c0IE97q"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/3JB9O01LGko"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -70,6 +70,7 @@ We want to recover if we want to handle a panic, stopping it from propagating up
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
|
||||
<span class="nx">recoverFromPanic</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -77,35 +78,12 @@ We want to recover if we want to handle a panic, stopping it from propagating up
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">recoverFromBuiltInPanic</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
<p>We see it because we recovered from a panic.</p>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">recoverFromCustomPanic</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
|
||||
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">"Finished without panicing."</span><span class="p">)</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
|
||||
@ -114,12 +92,13 @@ We want to recover if we want to handle a panic, stopping it from propagating up
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>defer is defined.</p>
|
||||
<p>recover is always defined in a defer.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">recoverFromBuiltInPanic</span><span class="p">(</span><span class="nx">i</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
|
||||
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">recoverFromPanic</span><span class="p">(</span><span class="nx">i</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
|
||||
<span class="k">defer</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -131,53 +110,7 @@ We want to recover if we want to handle a panic, stopping it from propagating up
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="k">defer</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
|
||||
<span class="k">if</span> <span class="nx">r</span> <span class="o">:=</span> <span class="nb">recover</span><span class="p">();</span> <span class="nx">r</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"Recovered. Error:\n"</span><span class="p">,</span> <span class="nx">r</span><span class="p">)</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">}()</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="kd">var</span> <span class="nx">a</span> <span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">int</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">"Getting index %d"</span><span class="o">+</span>
|
||||
<span class="s">" of array of len %d...\n"</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="nx">a</span><span class="p">))</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">"Item in index %d: %d"</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">a</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>defer is defined.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">recoverFromCustomPanic</span><span class="p">(</span><span class="nx">i</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="k">defer</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
|
||||
<span class="k">if</span> <span class="nx">r</span> <span class="o">:=</span> <span class="nb">recover</span><span class="p">();</span> <span class="nx">r</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
|
||||
<div class="highlight"><pre> <span class="k">if</span> <span class="nx">r</span> <span class="o">:=</span> <span class="nb">recover</span><span class="p">();</span> <span class="nx">r</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"Recovered. Error:\n"</span><span class="p">,</span> <span class="nx">r</span><span class="p">)</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">}()</span>
|
||||
@ -215,7 +148,8 @@ We want to recover if we want to handle a panic, stopping it from propagating up
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
<p>We won’t see this because we paniced.</p>
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
|
||||
@ -233,16 +167,16 @@ We want to recover if we want to handle a panic, stopping it from propagating up
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Running this program will exit correctly,
|
||||
even though panic was invoked in two methods.
|
||||
The recover is responsible for recovering from panics.</p>
|
||||
even though panic was called.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="gp">$</span> go run recover.go
|
||||
<span class="go">Getting index 10 of array of len 5...</span>
|
||||
<span class="go">About to process i=-1</span>
|
||||
<span class="go">Recovered. Error:</span>
|
||||
<span class="go"> runtime error: index out of range [10] with length 5</span>
|
||||
<span class="go"> Accepting only non-negative numbers but received -1</span>
|
||||
<span class="go">Finished without panicing.</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -254,13 +188,9 @@ The recover is responsible for recovering from panics.</p>
|
||||
to use error-indicating return values wherever possible.</p>
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
<td class="code empty">
|
||||
|
||||
<div class="highlight"><pre><span class="go">About to process i=-1</span>
|
||||
<span class="go">Recovered. Error:</span>
|
||||
<span class="go"> Accepting only non-negative numbers but received -1</span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@ -277,7 +207,7 @@ to use error-indicating return values wherever possible.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' recoverFromBuiltInPanic(10)\u000A');codeLines.push(' fmt.Println()\u000A');codeLines.push(' recoverFromCustomPanic(-1)\u000A}\u000A');codeLines.push('func recoverFromBuiltInPanic(i int) {\u000A');codeLines.push(' defer func() {\u000A if r :\u003D recover(); r !\u003D nil {\u000A fmt.Println(\"Recovered. Error:\\n\", r)\u000A }\u000A }()\u000A');codeLines.push(' var a [5]int\u000A fmt.Printf(\"Getting index %d\"+\u000A \" of array of len %d...\\n\", i, len(a))\u000A fmt.Printf(\"Item in index %d: %d\", i, a[i])\u000A}\u000A');codeLines.push('func recoverFromCustomPanic(i int) {\u000A');codeLines.push(' defer func() {\u000A if r :\u003D recover(); r !\u003D nil {\u000A fmt.Println(\"Recovered. Error:\\n\", r)\u000A }\u000A }()\u000A');codeLines.push(' fmt.Printf(\"About to process i\u003D%d\\n\", i)\u000A');codeLines.push(' if i \u003C 0 {\u000A panic(fmt.Errorf(\"Accepting only\"+\u000A \" non-negative numbers but received %d\", i))\u000A }\u000A');codeLines.push(' fmt.Printf(\"Doing something with %d\\n\", i)\u000A}\u000A');codeLines.push('');codeLines.push('');
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A recoverFromPanic(-1)\u000A');codeLines.push(' fmt.Printf(\"Finished without panicing.\")\u000A}\u000A');codeLines.push('func recoverFromPanic(i int) {\u000A defer func() {\u000A');codeLines.push(' if r :\u003D recover(); r !\u003D nil {\u000A fmt.Println(\"Recovered. Error:\\n\", r)\u000A }\u000A }()\u000A');codeLines.push(' fmt.Printf(\"About to process i\u003D%d\\n\", i)\u000A');codeLines.push(' if i \u003C 0 {\u000A panic(fmt.Errorf(\"Accepting only\"+\u000A \" non-negative numbers but received %d\", i))\u000A }\u000A');codeLines.push(' fmt.Printf(\"Doing something with %d\\n\", i)\u000A}\u000A');codeLines.push('');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user