panic for multilevel returns

go

Solution

I use this approach myself in my parsers. I don't panic with an integer value though. I use the actual current error as the panic message. The top level call doing the `recover()` simply appends some file/line/column information and then returns it as a regular `error`.

This method, and just returning errors from all functions are the only ways to do this in Go. The panic approach is a great deal more effective for the parser case, as it makes the lexer rules considerably simpler to implement (and read) as there are no `if err != nil { return }` parts littered everywhere.

Problem

Is there a better way to return from long sequences of recursive function calls? I currently panic with a marker value like this: ``` type exitNow int ... panic(exitnow(0)) ``` to return multiple levels at once. At the root function a call to recover does general error handling (turning panics into errors) and handles exitNow as a special case. This works fine I just want to know if there is a better way. I already use a bool return value for a related purpose but using another one for this would be a pain. (every call to every function would need an if statment) If it helps any this is part of the implimentation of a recusive decent parser.

Original source