Can I recover and retry a panic?

(Wyattb) #1

How do I recover and continue from a panic (E.g. resume where I left off? Could do something like this (pseudocode):

func main() { 
   defer errorfix()
   ....'overflow' panic
   ....'divide by zero' panic

func errorfix() {
   if err := recover(); err !=nil { 
     ... fix something based on err and then 
     return to retry
     // or return to next line
     // or panic('unknown error')

(Donghao) #2

I think not. Once current program run into a panic, it would reclaim the stack your function (in your code, the main() ) had claimed, so you would not get the chance.

(Sandertv) #3

Consider embedding the code in an anonymous function so that panic-ing, then defer recovery only exits that function.