C# exception handler resume next

2019-09-15 03:36发布

问题:

I’ve been investigating how I can alter the behaviour of c# method execution specifically when an exception occurs to support:

Retry/Continue: to be able to try the same statement again and carry on once successful Skip/Resume: moves to the next statement and continues with execution

I’ve read the many responses that this is poor coding practice, but this is for a code converter, which is converting millions of lines of code from a language where this functionality is supported. I need this to be functionally consistent.

回答1:

Your only option could be to adopt a (frankly horrible) style like this:

var done = false;
while (!done) { try { line1(); done = true; } catch {} }
done = false;
while (!done) { try { line2(); done = true; } catch {} }
// etc

Mixed with:

try { line1(); } catch {}
try { line2(); } catch {}
// etc

Rest assured that having millions of such lines will make it very hard and annoying to maintain for the rest of its life.