panic_recover.go 422 B

1234567891011121314151617181920212223242526272829303132
  1. // panic_recover.go
  2. package main
  3. import (
  4. "fmt"
  5. )
  6. func badCall() {
  7. panic("bad end")
  8. }
  9. func test() {
  10. defer func() {
  11. if e := recover(); e != nil {
  12. fmt.Printf("Panicing %s\r\n", e)
  13. }
  14. }()
  15. badCall()
  16. fmt.Printf("After bad call\r\n") // <-- wordt niet bereikt
  17. }
  18. func main() {
  19. fmt.Printf("Calling test\r\n")
  20. test()
  21. fmt.Printf("Test completed\r\n")
  22. }
  23. /* Output:
  24. Calling test
  25. Panicing bad end
  26. Test completed
  27. */