go - Golang panic crash prevention -


in golang panic without recover crash process, end putting following code snippet @ beginning of every function:

defer func() {     if err := recover(); err != nil {         fmt.println(err)     } }() 

just in order prevent program crashing. i'm wondering, way go? because think looks little bit strange put same code everywhere.

it seems me, java way, bubbling exceptions calling function, until main function better way control exceptions/panics. understand it's go's design, advantage of crashing process go does?

you should recover panic if know why. go program panic under 2 circumstances:

  • a program logic error (such nil pointer dereference or out-of-bounds array or slice access)
  • an intentional panic (called using panic(...)) either code or code code calls

in first case, crash appropriate because means program has entered bad state , shouldn't keep executing. in second case, should recover panic if expect it. best way explain it's extremely rare, , you'll know case if see it. i'm positive whatever code you're writing, don't need recover panics.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -