-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patherror.go
53 lines (45 loc) · 884 Bytes
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"errors"
"fmt"
"os"
"strconv"
)
// Custom error message with errors.New()
func check(a, b int) error {
if a == 0 && b == 0 {
return errors.New("this is a custom error message")
}
return nil
}
// Custom error message with fmt.Errorf()
func formattedError(a, b int) error {
if a == 0 && b == 0 {
return fmt.Errorf("a %d and b %d. UserID: %d", a, b, os.Getuid())
}
return nil
}
func main() {
err := check(0, 10)
if err == nil {
fmt.Println("check() ended normally!")
} else {
fmt.Println(err)
}
err = check(0, 0)
if err.Error() == "this is a custom error message" {
fmt.Println("Custom error detected!")
}
err = formattedError(0, 0)
if err != nil {
fmt.Println(err)
}
i, err := strconv.Atoi("-123")
if err == nil {
fmt.Println("Int value is", i)
}
i, err = strconv.Atoi("Y123")
if err != nil {
fmt.Println(err)
}
}