4 ms·
Is there any way to 'catch' a divide by zero panic in go?
by pixie_ 13y ago
Is there any way to 'catch' a divide by zero panic in go?
- Queue29 13y agoSure, http://play.golang.org/p/dAQ01dus9Y http://play.golang.org/p/dAQ01dus9Y
- chimeracoder 13y agoFor others: note that that's only because Queue29 assigned 0 to a variable before using it as the denominator, so there's no way for Go to catch that at compile-time. If you divide by a literal 0, this is a compile-time error, so there's no panic (and no need to recover()): http://play.golang.org/p/E0jSDAHwtg http://play.golang.org/p/E0jSDAHwtg
- pixie_ 13y agoInteresting thanks. Of course then I was wondering what this recover() stuff was and I found an explanation here - http://blog.golang.org/defer-panic-and-recover http://blog.golang.org/defer-panic-and-recover cool stuff.