Concise C# Part 2 : Try Catch and reusability

Exception handling is an important part of any software. In this post, I will talk about a trick to make but some kind of reusability on exception handling.

This post is part of a serie about making C# code shorter

When you write code, you usually try to reuse your code by creating methods and calling those methods each time you the need their functionality. When working with exceptions, it’s a bit tricky because, they don’t follow the normal code flow, they jump out of your method … unless you catch them. The consequence is that you usually have multiple similar try {} catch {} blocks that are often copied and pasted.

This is what usually happens

The solution to reuse the exception block

This is what you could do to reuse the same error handling code block in c# :

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">