2014-10-05 1 views
3

이전 자바 7 확인 향상된 유형에 예외를 Rethrowing, 우리는 두 가지 방법 중 하나를 수행해야합니다JDK 7 다중 예외 유형 잡기와

public void rethrowException(String exceptionName) throws FirstException, SecondException{ 
    try { 
     if (exceptionName.equals("First")) { 
     throw new FirstException(); 
     } else { 
     throw new SecondException(); 
     } 
    } catch (FirstExceptione) { 
     throw e; 
    }catch (SecondException) { 
     throw e; 
    } 
    } 

와 두 번째 존재, 나의 이해, 새로운 자바 7.0으로 당

public void rethrowException(String exceptionName) throws Exception { 
    try { 
     if (exceptionName.equals("First")) { 
     throw new FirstException(); 
     } else { 
     throw new SecondException(); 
     } 
    } catch (Exception e) { 
     throw e; 
    } 
    } 

당신이 예외의 예외의 다양한 수준을 잡을 수있는 방법으로 개선하고 여전히 같은 방법 정의의 좁은 예외를 유지하고있다 코드 아래

public void rethrowException(String exceptionName) 
    throws FirstException, SecondException { 
    try { 
     // ... 
    } 
    catch (Exception e) { 
     throw e; 
    } 
    } 

자바 SE 7 컴파일러는 문 던져 전자에 의해 던져진 예외가 이 try 블록에서 온해야한다는 것을 확인할 수 있으며, try 블록에서 throw 유일한 예외는 FirstException 및 SecondException 수 있습니다. catch 절의 예외 매개 변수 e가 Exception 유형 인 경우에도 컴파일러는 FirstException 또는 SecondException의 인스턴스인지 여부를 확인할 수 있습니다. catch 매개 변수가 catch 블록의 다른 값에 지정되면이 분석을 사용할 수 없습니다. 그러나 catch 매개 변수가 다른 값에 할당 된 경우 의 예외 유형 Exception을 메서드 선언의 throws 절로 지정해야합니다.

오라클 문서에서

, 상세

, 당신은 캐치 절, 하나 개 이상의 예외 유형을 선언하고이 catch 블록에 의해 처리 예외를 다시 발생하면 자바 SE 7의 뒷부분에, 컴파일러가 있는지 확인합니다 재 throw 예외의 유형은 다음과 같은 조건을 충족 :

1) The try block is able to throw it. 
2) There are no other preceding catch blocks that can handle it. 
3) It is a subtype or supertype of one of the catch clause's exception parameters. 
4) In releases prior to Java SE 7, you cannot throw an exception that is a supertype of one of 
the catch clause's exception parameters. A compiler from a release prior to Java SE 7 generates 
the error, "unreported exception Exception; must be caught or declared to be thrown" at the 
statement throw e. The compiler checks if the type of the exception thrown is assignable to any 
of the types declared in the throws clause of the rethrowException method declaration. However, 
the type of the catch parameter e is Exception, which is a supertype, not a subtype, of 
FirstException andSecondException. 

내가 theoritically 3과 4rth 점을 undertand에 실패했습니다. 누군가가 위에서 언급 한 코드를 통해 나를 설명 할 수 있습니까?

답변

2

상황은 다음 고려 :

귀하의 응용 프로그램이 예외 FirstException, SecondException 중 하나가 발생,

ExceptionExceptionFirstException의 슈퍼입니다 SecondException을 그들이 Exception을 확장하기 때문이다.

또한 SecondException extends FirstException을 적용해야합니다. 따라서 FirstExceptionSecondExeption의 수퍼 유형이고 SecondException 하위 유형은 FirstException입니다.

이제는 항상 SecondException을 던지는 방법이 있습니다.

첫 번째 케이스 :

try { 
[...] 
} catch(SecondException se) { 
// Exception gets always caught in here 
[...] 
} catch(FirstException fe) { 
[...] 
} catch(Exception e) { 
[...] 
} 

두 번째 케이스 :

try { 
[...] 
} catch(Exception e) { 
// Exception gets always caught in here 
// because Exception is supertype of all other Exception 
[...] 
} catch(FirstException fe) { 
[...] 
} catch(SecondException se) { 
// is never called. 
[...] 
} 

당신이 포인트를 볼 수 있나요?

+0

나는 이것을 알고있다.두 번째 경우는 예외가 항상 catch (Exception e) {// ....} –

+0

에 의해 잡힐 것이므로 나에 따르면 컴파일 시간 오류가 될 것입니다. 예외는 매우 일반적으로 일반에서 호출됩니다. 그래서 매우 일반적인 예외 "예외"를 잡으면 "SecondException"과 "FirstException"이 해당 예외 상황에 포함됩니다. 따라서 예외 블록은 입력되지 않습니다. – sxleixer