-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3ec819
commit 6b6ab58
Showing
8 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
|
||
func main() { | ||
defer func() { | ||
r := recover() | ||
println("recover:", r) | ||
}() | ||
|
||
arr := []int{1, 2, 3} | ||
_ = arr[3] // Panics because index 3 is out of bounds | ||
} | ||
|
||
// Output: | ||
// recover: slice index out of bounds: 3 (len=3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
|
||
func main() { | ||
defer func() { | ||
r := recover() | ||
println("recover:", r) | ||
}() | ||
|
||
arr := []int{1, 2, 3} | ||
_ = arr[-1:] // Panics because of negative index | ||
} | ||
|
||
// Output: | ||
// recover: invalid slice index -1 (index must be non-negative) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
|
||
func main() { | ||
defer func() { | ||
r := recover() | ||
println("recover:", r) | ||
}() | ||
|
||
x, y := 10, 0 | ||
_ = x / y // Panics because of division by zero | ||
} | ||
|
||
// Output: | ||
// recover: division by zero |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
|
||
func main() { | ||
defer func() { | ||
r := recover() | ||
println("recover:", r) | ||
}() | ||
|
||
var i interface{} = "hello" | ||
_ = i.(int) // Panics because i holds a string, not an int | ||
} | ||
|
||
// Output: | ||
// recover: string is not of type int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
|
||
func main() { | ||
defer func() { | ||
r := recover() | ||
println("recover:", r) | ||
}() | ||
|
||
_ = make([]int, -1) // Panics because of negative length | ||
} | ||
|
||
// Output: | ||
// recover: len out of range |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
|
||
func main() { | ||
defer func() { | ||
r := recover() | ||
println("recover:", r) | ||
}() | ||
|
||
str := "hello" | ||
_ = str[10] // Panics because index 10 is out of bounds | ||
} | ||
|
||
// Output: | ||
// recover: index out of range [10] with length 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
|
||
func main() { | ||
defer func() { | ||
r := recover() | ||
println("recover:", r) | ||
}() | ||
|
||
var m map[string]int // nil map | ||
m["key"] = 42 // Panics when trying to assign to a nil map | ||
} | ||
|
||
// Output: | ||
// recover: uninitialized map index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
|
||
func main() { | ||
defer func() { | ||
r := recover() | ||
println("recover:", r) | ||
}() | ||
|
||
var p *int | ||
println(*p) | ||
} | ||
|
||
// Output: | ||
// recover: nil pointer dereference |