Skip to content

Latest commit

 

History

History
52 lines (41 loc) · 929 Bytes

how-to-catch-recover-panic-error.md

File metadata and controls

52 lines (41 loc) · 929 Bytes

How to catch (recover) panic() error

package main

func main() {
  test()
  print("world after panic")
}

func test() {
  defer func() {
		if err := recover(); err != nil {
			print("catched panic, ha ha ha...")
		}
	}()
	
	panic("noooo")
}
  • package main - default package declaration
  • func main() { - declare main function that will be launched automatically
  • func test() - test function has panic() call and its handler via recover()
  • if err := recover() - catch panic via recover()
  • print("world after panic") - this should would if panic was catches successfully

group: panic

Example:

package main

func main() {
  test()
  print("world after panic")
}

func test() {
  defer func() {
		if err := recover(); err != nil {
			print("catched panic, ha ha ha...")
		}
	}()
	
	panic("noooo")
}
catched panic, ha ha ha...world after panic