다음 코드가 제대로 컴파일되지만 호출되는 메서드가 Exception
을 내포하지 않아도되는 이유는 무엇입니까? Exception
이 검사 예외이며 확인되지 않은 예외가 아닌가? 명확히하십시오. 내가 IOexception
와 시도 캐치 (체크 예외를) 사용하려면예외가 잡히지는 않지만 IOException은 발생합니다
class App {
public static void main(String[] args) {
try {
amethod();
System.out.println("try ");
} catch (Exception e) {
System.out.print("catch ");
} finally {
System.out.print("finally ");
}
System.out.print("out ");
}
public static void amethod() { }
}
, 방법은 IOException
을 던질 필요를 호출되고. 나는 이걸 얻는다.
import java.io.IOException;
class App {
public static void main(String[] args) {
try {
amethod();
System.out.println("try ");
} catch (IOException e) {
System.out.print("catch ");
} finally {
System.out.print("finally ");
}
System.out.print("out ");
}
public static void amethod() throws IOException { }
}
귀하의 이해가 거꾸로 생각됩니다. 'throws' 선언은 호출자가 걸러 낼 예외가 아니라 호출 수신자가 던져 낼 체크 된 예외를 나열하기위한 것입니다. –