errors
# Node.js
const err1 = new Error("some error");
console.log(err1);
class FooError extends Error {
constructor(message) {
super(message);
this.name = "FooError";
this.message = message;
}
toString() {
return this.message;
}
}
const err2 = new FooError("my custom error");
console.log(err2);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Output
Error: some error
{ FooError: my custom error }
1
2
2
# Go
package main
import (
"errors"
"fmt"
)
type FooError struct {
s string
}
func (f *FooError) Error() string {
return f.s
}
func NewFooError(s string) error {
return &FooError{s}
}
func main() {
err1 := errors.New("some error")
fmt.Println(err1)
err2 := NewFooError("my custom error")
fmt.Println(err2)
}
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
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
Output
some error
my custom error
1
2
2
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22