vendredi 22 décembre 2017

Pattern stop service

I have service that process files all the day, and I catch every exception (log it) in order to do not stop the service and just skip this file. But I would like to stop the service if there are 10 errors occured in a row. I don't want just have an variable that increment when errors occured and set to zero when everything was success because I have a lot of catches and places where I have to set the variable to zero. (see code example)

I've heard that there is exist pattern on how to stop my service in case of several exception occured. But I don't know details.

Question: how would you handle this case?

E.g.:

try 
{
  switch(switchVariable) 
    {
      case "1":
        Method1();
        Method2();
        Method3();
        //If we are here than everything OK.
        break;
      case "2":
        Method1();
        Method2();
        Method3();
        //If we are here than everything OK.
        break;
      ...
}
catch(SpecificException1 ex)
{
  DoSomething1();
}
catch(SpecificException2 ex)
{
  DoSomething2();
}
catch(SpecificException3 ex)
{
  DoSomething3();
}
catch(Exception ex)
{
}

Aucun commentaire:

Enregistrer un commentaire