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

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -