You could create a SkipOnError method like this:
private SkipOnError(Action action)
{
try
{
action();
}
catch
{
}
}
Then you could call it like so:
try
{
SkipOnError(() => /*line1*/);
line2;
line3;
} catch {}
Edit: This should make it easier to skip a given exception:
private SkipOnError(Action action, Type exceptionToSkip)
{
try
{
action();
}
catch (Exception e)
{
if (e.GetType() != exceptionToSkip) throw;
}
}
NOTE: I'm not actually suggesting you do this - at least not on a regular basis, as I find it rather hacky myself. But it does sort of show off some of the functional things we can now do in C#, yay!
What I would really do is this: Refactor line1
into a method (Extract Method). That new method should handle any foreseeable exceptions (if they can be handled) and thus leave the caller in a known state. Because sometimes you really want to do line1
, except, maybe it's ok if an error happens...