C# exception handler resume next
Asked Answered
O

1

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.

Oophorectomy answered 13/2, 2017 at 10:32 Comment(4)
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.Charteris
@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.Oophorectomy
Well, don't keep it a secret, there is no point to that. Unless you prefer not getting the best answer.Charteris
@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.Oophorectomy
U
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.

Unpaid answered 13/2, 2017 at 10:52 Comment(3)
Yes, and +1 for the word 'horrible'. But for a generator this might be feasible.Pistole
Indeed, horrible to maintain. I've looked at Polly along similar lines. Doesnt support resume next, only retry.Oophorectomy
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?Oophorectomy

© 2022 - 2024 — McMap. All rights reserved.