title: Java Exception handling for below use case
body: <p>I have below code.</p>
  
  <p>The question is  : </p>
  
  <p>Is there a better way to handle exception for below use case other than this ?</p>
  
  <p>My particular interest is using <code>handleException</code> method. </p>
  
  <pre><code>public void checkSomeBehaviour() throws Exception{
  
      try{
  
          someBusinessValidation() ; 
          //this method can throw BusinessValidationException which is subclass of Exception 
          //BusinessValidationException contains code in getMessage() method
  
  
          try{    
              doSomething();  
          }catch(Exception ex1){  
                  //throw Exception object with specific error code say 123. So this needs to be wrapped in separate try/catch
  
  
                  throw CustomException('123');    //CustomException is subclass of Exception
          }       
          try{    
              doSomethingElse();      
          }catch(Exception ex2){  
              //throw Exception object with specific error code say 456 .So this needs to be wrapped in separate try/catch
              throw CustomException('456');       //CustomException is subclass of Exception  
          }
  
      }   
      catch(Exception ex){
  
          //Based on Exception type , a common exception needs to be thrown as ValidationException 
          handleException(ex);
  
      }
  
  
  }
  //this method inspects exception type and does appropriate action accordingly  
  private void handleException(Exception ex){
  
  
      if(ex instanceof CustomException){
          throw new ValidationException(ex.getCode());
      }else if(ex instanceof BusinessValidationException){    
          throw new ValidationException(ex.getMessage());
      }else{
              throw new ValidationException(100); //throw code as 100 since this is generalised exception
      }
  
  }
  </code></pre>