0

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.

JohnB
  • 1
  • 1
  • The implementation of VB.NET's `On Error Resume Next` is just as dreadful as you'd imagine. Only good advice is to not convert, the source language just doesn't matter much in .NET. Or do it mechanically with a language converter. Or to consider *why* you are converting, surely it is to fix the rather broken existing code? It is broken, don't repeat the same mistakes. – Hans Passant Feb 13 '17 at 12:08
  • @Hans - the language this is being converted from is not VB based, and furthermore the existing code is not broken as such. Still there are solid reasons to convert anyway, if it is possible, and thats what Im trying to establish here. – JohnB Feb 13 '17 at 20:14
  • Well, don't keep it a secret, there is no point to that. Unless you prefer not getting the best answer. – Hans Passant Feb 13 '17 at 20:16
  • @Hans - just felt the original technology isnt relevant to the c# query, otherwise it's at risk of going down the path of being told to stick with what we have, or to refactor it all to something more 'best practice'. I'm trying to determine how difficult a hands-off conversion would be to write at this point, and knowing how big a job that is and what we'd endup with can then be factored into pros/cons of proceeding. Exception handling could be a sticking point. – JohnB Feb 13 '17 at 23:11

1 Answers1

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.

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • Yes, and +1 for the word 'horrible'. But for a generator this might be feasible. – H H Feb 13 '17 at 11:15
  • Indeed, horrible to maintain. I've looked at [Polly](http://www.thepollyproject.org) along similar lines. Doesnt support resume next, only retry. – JohnB Feb 13 '17 at 11:31
  • Any thoughts on whether either code or IL weaving could inject this functionality in? I may be able to convert to the above format as is but conceivably weaving could address the maintenance burden left behind too? – JohnB Feb 16 '17 at 05:12