try/catch
try/catch
Node.js
function foo(fail) {
if (fail) {
throw Error('my error')
}
}
function main() {
try {
foo(true)
} catch(err) {
console.log(`caught error: ${err.message}`)
}
}
main()
Output
caught error: my error
Go
package main
import (
"errors"
"fmt"
)
func foo(fail bool) error {
if fail {
return errors.New("my error")
}
return nil
}
func main() {
err := foo(true)
if err != nil {
fmt.Printf("caught error: %s\n", err.Error())
}
}
Output
caught error: my error
