Reducing duplicate error handling code in C#?

This looks like an excellent opportunity to have a look at Aspect Oriented Programming. Here is a good article on AOP in .NET. The general idea is that you’d extract the cross-functional concern (i.e. Retry for x hours) into a separate class and then you’d annotate any methods that need to modify their behaviour in that way. Here’s how it might look (with a nice extension method on Int32)

[RetryFor( 10.Hours() )]
public void DeleteArchive()
{
  //.. code to just delete the archive
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)