exceptions
# Node.js
function foo() {
throw Error("my exception");
}
function main() {
foo();
}
process.on("uncaughtException", (err) => {
console.log(`caught exception: ${err.message}`);
process.exit(1);
});
main();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Output
caught exception: my exception
1
# Go
package main
import (
"fmt"
)
func foo() {
panic("my exception")
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("caught exception: %s", r)
}
}()
foo()
}
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
caught exception: my exception
1
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22