4 ms·
If you're talking about panics, you can only handle them in defer funcs if I'm not mistaken, which makes them significantly different from exceptions that I'm u
by collinvandyck76 12y ago
If you're talking about panics, you can only handle them in defer funcs if I'm not mistaken, which makes them significantly different from exceptions that I'm used to that can be caught in any part of the code. The dependency on defer in my opinion makes them unusable as typical exceptions in my case. This was perhaps the intent, making panics and the recoveries thereof truly exceptional and painful, pushing one towards the normal error handling semantics, which is what I dislike.
- cmelbye 12y agoIt's true that you can only handle them in defer funcs, but the defer func could be anywhere up the stack, not just the current function you're inside. (I'm not sure if that was your understanding or not) "The panic and recover functions behave similarly to exceptions and try/catch in some other languages in that a panic causes the program stack to begin unwinding and recover can stop it. Deferred functions are still executed as the stack unwinds. If recover is called inside such a deferred function, the stack stops unwinding and recover returns the value (as an interface{}) that was passed to panic." https://code.google.com/p/go-wiki/wiki/PanicAndRecover https://code.google.com/p/go-wiki/wiki/PanicAndRecover
- dragonwriter 12y ago> If you're talking about panics, you can only handle them in defer funcs if I'm not mistaken, That's true...but no different from "exceptions can only be caught in catch blocks". > which makes them significantly different from exceptions that I'm used to that can be caught in any part of the code. It doesn't make them any different from exceptions -- just as a catch block can be anywhere up the call chain, a deferred function can have been set anywhere up the chain. Defer is basically "finally", except the position is different, and "recover" inside it lets it also do what "catch" does.