Search code examples
javaspringexceptionhttpexception

exceptions handling error scenario


I have the following scenario:

  Class C{
  mainCall(){
try{
     //do something;
 }catch(MyException e)
     //doSomethingElse;
  }
}

Class A{
methodOne() throws myException{
 b.callMethodTwo();
}
}

class B{
callMethodTwo() throws myException{
  try {
            value = callService()//call some service
        } catch(HttpClientErrorException | HttpServerErrorException e){
            throw new MyException(e);
        }
 return value;
   }
}

if some exceptions occured in callMethodTwo() (not HttpClientErrorException or HttpServerErrorException). What would be the flow in this case? Would the catch part in mainCall() in method C execute? I have almost 5 chain call in my code but tried to simplify here and generated this sceario.


Solution

  • if the statement value = callService(); in the method callMethodTwo throws

    1. an exception of type HttpClientErrorException or HttpServerErrorException or their subtypes, it will be caught by the following catch block. This catch block will throw a new exception of type MyException which will be bubble up to methodOne which in turn bubble it up to mainCall where it will be finally caught by the catch block.
    2. an exception of type MyException or its subtypes, it will be bubbled up from callMethodTwo to callMethodOne to mainCall where it will be finally caught by the catch block.
    3. an exception of any other type, it will be bubbled up from callMethodTwo to callMethodOne to mainCall to the calling method of mainCall.