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() { 
   ...code
   defer errorfix()
   ....code..
   ....'overflow' panic
   ....code...
   ....'divide by zero' panic
   ....code...
}

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.