lundi 8 février 2016

What is a better design, should we throw exceptions or should we send dummy objects with empty result

I have this simple scenario. Design 1 :

@RequestMapping("/myApiCall")
public MyResponse thisIsSomeApiCall(MyRequest request) throws BusinessException{
    try{
        myService.getMyResponse(request);
    }catch(BusinessException e){
        throw e;
    }
}

In this i am throwing a exception if some business logic fails.

Design 2

@RequestMapping("/myApiCall")
public MyResponse thisIsSomeApiCall(MyRequest request) throws BusinessException{
    try{
        myService.getMyResponse(request);
    }catch(BusinessException e){
        MyResponse response = new MyResponse();
        response.setError(true);
        response.setResult(null);
        return response;
    }
}

In this i am returning a demo kind of response which says that some error is in the response.

I am wondering which is a better design(design 1 or design 2) and why??

Aucun commentaire:

Enregistrer un commentaire