Posted Mar 29, 2012 7:10 UTC (Thu) by imgx64 (guest, #78590)
In reply to: Uses by Cyberax
Parent article: Go version 1 released
> 2) Exceptions in Go are FUBARed
Are you using panic/recover like exceptions in other languages? They're not supposed to be used that way; you should use multiple return values instead.
panic()/recover() are usually used:
1- Internally in a package, if you have a long call chain (such as in a recursive descent parser), and adding ", err" to each call is tedious. You can panic on errors, and then catch the panic at the topmost function *and convert it to an error before returning it*. It's unidiomatic to let such panics escape your package.
2- To report genuine *programmer* errors. For example, regexp.MustCompile panics on invalid regular expressions because they are meant to be used with fixed strings (as opposed to regular expressions that are created dynamically or from user input).
Some programmers use recover() to "increase reliability" by catching all possible panics. I find that rather useless, since the whole program will crash if a panic reaches the top of *any goroutine*, and no recover can help. It's usually easier to have a daemon process respawn the program (after logging the crash, of course).