2012-12-29 6 views
0

가능한 중복 :
Is there a way to throw an exception without adding the throws declaration?어떻게 예외를 되돌릴 수 있습니까?

Exception을 캡슐화 및 방법이 이미 정의되어 throws 조항이없는 경우 다른 Exception에서 그것을 다시 발생 할 수있는 방법이 있는지 궁금.

예 (JMS와 onMessage 방법) :

public void onMessage(Message message) { 

    if(message instanceof ObjectMessage){ 

     ObjectMessage objectMessage = (ObjectMessage)message; 

     try { 
      Object object = objectMessage.getObject(); 
     } catch (JMSException e) { 
      throw new CustomException("blah blah", e); // ERROR HERE : Unhandled exception type CustomException 
     } 

    } 

} 

그래서, 어떻게하십시오 캡슐화 내 코드에 CustomException를 전달할 수 있습니다

? 방법이 있습니까? 감사합니다

+0

예입니다. 죄송합니다 –

답변

4

RuntimeException의 하위 클래스를 사용해야합니다.

+0

나는 그것을 할 것입니다. 고맙습니다 –

3

메서드에서 예외를 선언하지 않으면 유일한 예외는 RuntimeException의 하위 클래스 인 예외를 throw하는 것입니다.

CustomException의 경우 RuntimeException을 연장해야합니다.

이론의 비트 Oracle 예외.

1

CustomException이 확인되지 않은 예외 (런타임 예외) 인 경우이 작업이 성공했을 것입니다. 확인 된 대 체크되지 않은 예외에 대해서는 여기에서 읽을 수 있습니다. - Java: checked vs unchecked exception explanation

관련 문제