exceptions


Node.js

  1. function foo() {
  2. throw Error('my exception')
  3. }
  4. function main() {
  5. foo()
  6. }
  7. process.on('uncaughtException', err => {
  8. console.log(`caught exception: ${err.message}`)
  9. process.exit(1)
  10. })
  11. main()

Output

  1. caught exception: my exception

Go

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func foo() {
  6. panic("my exception")
  7. }
  8. func main() {
  9. defer func() {
  10. if r := recover(); r != nil {
  11. fmt.Printf("caught exception: %s", r)
  12. }
  13. }()
  14. foo()
  15. }

Output

  1. caught exception: my exception