Notes on ‘Refactoring ‘ — 5.2 Replace Exception with Test

Before Refactoring

   try{
      return list.get(index);
   }catch(IndexOutOfBoundsExceptione){
      return null;
   }

After Refactoring

   if(index >= list.size()){
      return null;
   }
   return list.get(index);


Benefits: Avoid too many exceptions since it is not so good to read and it should only be used for an exception flow.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.