2011-11-18 2 views
1

이 방법이 있습니다. 문제는이 조건이catch 블록이 호출 된 경우에도 코드가 실행 중입니다.

if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1) 

을 충족 될 때 그것은 여기 catch 블록에

catch (Exception e) { 

     this.errorText = e.getMessage().toString(); 
     info.setErrorText(this.errorText.toString()); 
     response.setinfo(info); 

    } 

을 것입니다하지만 여전히

final boolean toProceedorNot = validate(bagList.toArray(new Bag[bagList.size()])) 

입니다 다음 줄을 execuing 것이 무엇인지 이것이 맞는다면 원하는가이다.

if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1) 
**then directly return the response;** 

public Response getData(Request request) { 

    Info info = new Info(); 

    Response response = new Response(); 
    String xmlrequest = request.getxmlMessage(); 

    HashMap listMap = new HashMap(); 
    List<Ungar> UngarList = new ArrayList<Ungar>(); 
    List<Bag> bagList = new ArrayList<Bag>(); 

    UniverseStaxParser xmlparser = new UniverseStaxParser(); 
    try { 
     listMap = (HashMap) xmlparser.parseData(xmlrequest); 

     UngarList = (List<Ungar>) listMap.get("UngarItems"); 

     bagList = (List<Bag>) listMap.get("bagItems"); 


     if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1) 
      throw new Exception("No Valid Data is passed as Input "); 

    } catch (Exception e) { 

     this.errorText = e.getMessage().toString(); 
     info.setErrorText(this.errorText.toString()); 
     response.setinfo(info); 

    } 

    final boolean toProceedorNot = validate(bagList.toArray(new Bag[bagList.size()])); 


    try { 
     if (!toProceedorNot) { 
      info.setErrorText(errorText); 
      response.setinfo(info); 

     } else { 

      // some logic here goes 
     } 
    } catch (Exception e) { 
     errorText = e.getMessage().toString(); 
     info.setErrorText(errorText); 
     response.setinfo(info); 
    } 



    return response; 
} 

답변

6

이 왜 그 라인을 실행하지 것이다 나의 방법이다? 그들은 try/catch 밖에 있으며, 정상적인 프로그램 실행 흐름을 방해하는 것은 없습니다.

메소드에서 복귀하지 않으면 (또는 그렇지 않으면 제어 흐름을 변경하지 않는 한) 실행은 catch 블록 다음의 명령문에서 계속됩니다.

catch 블록에서 응답을 반환하려는 경우 catch 블록에서 Response을 반환하십시오.

그러나 이것은 일반적인 목적의 Exception을 잘 사용하고 있다는 것을 확신하지 못합니다.

1

난 당신이 다시 디자인을해야한다는 소프트웨어의이 부분을 생각해야 : 예외가 발생합니다이 메소드를 호출 null입니다

if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1) 

bagList합니다. UngarListnull이면 메서드를 호출하면 예외가 발생합니다. 정말 그럴 필요가 없습니다.

는 이러한 null 있는지 여부에 불확실성이있다 - 몇 줄 위에, 당신은 그들에게 새로운 가치를 할당하고 거의 즉시이 새로 만든 객체에 대한 참조를 잃고, 참조를 덮어 씁니다. 그건 옳지 않은 것 같아.

어떤 조건이 실제로 예외이며 어떤 조건이 발생할 것으로 예상되는지 파악하고 간단한 것을 다르게 처리하도록하십시오.

+0

null 객체의 메소드를 호출하면 널 포인터 예외가 발생합니다. 이것은 일반적인 실수입니다. – Jasonw

+0

대단히 고맙습니다. 코드를 수정하여 하나의 try 블록 아래에 보관했습니다. 이제는 작동합니다. – Revathi

관련 문제