Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classes used to manage exception

Is it good to have a class with short methods used to catch Exceptions?

class ContractUtils{
  public static String getCode(Contract contract) throws MyException{
    try{
      return contract.getInfo().getCode(); //throws ContractException and LogicException
    }catch(Exception e){
      throw new MyException("error during code reading:"+e.getMessage, e);
    }
  }
  //other methods like above...
}
like image 379
raythurnevoid Avatar asked Oct 15 '15 18:10

raythurnevoid


1 Answers

If a custom exception provides additional context or value to the calling code, then it's useful and encouraged to create them.

You're approach to using static methods is totally acceptable. Here's a good SO answer defining use cases for short static methods.

like image 92
Keith Avatar answered Nov 15 '22 12:11

Keith