Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting of hard to read try..catch..finally blocks? [closed]

How are you formatting your try..catch.finally blocks? Especially when only wrapping it around a small amount of code, it blows everything and makes code pretty unreadable and unsightly in my opinion.

Such as:

try
{
     MyService service = new Service();
     service.DoSomething();
     return something;
}
catch (Exception ex)
{
     LogSomething();
     return somethingElse;
}
finally
{
     MarkAsComplete();
     service.Dispose();
}

These 7 lines of code turned into a 16-line mess.

Any suggestions on better try..catch..finally formatting?

like image 684
Alex Avatar asked Jun 02 '09 00:06

Alex


1 Answers

Actually, this reads very well to me. If your actual code looks a lot like this, then I really wouldn't worry about it. It is VERY clear what is happening.

If your actual code is more complex, then consider breaking the blocks into well-named methods.

like image 186
Brian Genisio Avatar answered Sep 29 '22 09:09

Brian Genisio