3 ms·
Go emphasizes (and makes easy) a deliberately inline way of handling expected errors. So panics are for unexpected, normally non-recoverable errors. Recover al
by JulianMorrison 16y ago
Go emphasizes (and makes easy) a deliberately inline way of handling expected errors. So panics are for unexpected, normally non-recoverable errors. Recover allows one portion of code to be sandboxed against panics in another. Sandboxes are the atypical case.
What this means is that you can look at arbitrary Go code and know that it contains no "action at a distance". It exits where it says "return". Control flow is explicit.
Contrast Java, where I can't see what will throw unless I pull the code up in Eclipse and use "mark occurrences" feature on the declared exception - and it can be surprising. Oh and also, there could be exceptions thrown and not declared, if they are "runtime". Even Eclipse doesn't help much there.
Panic in Go is closer to Java's Error. Catching Error is usually a bad idea.