Can I recover and retry a panic?

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')
   }
}

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.

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

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.