getCause() 메소드는 Throwable 에서 가져옵니다. 클래스이며 원인 을 반환하는 이 메서드를 사용할 수 있습니다. 예외 또는 반환 널 예외의 원인을 알 수 없는 경우. getCause() 메서드는 인수를 허용하지 않으며 예외를 throw하지 않습니다. 생성자 중 하나가 제공했거나 initCause() 형식에 의해 결정된 원인을 반환합니다. 던질 수 있는 방법 수업.
구문
public Throwable getCause()
예시
public class GetCauseMethodTest {
public static void main(String[] args) throws Exception {
try {
myException();
} catch(Exception e) {
System.out.println("Cause = " + e.getCause());
}
}
public static void myException() throws Exception {
int arr[] = {1, 3, 5};
try {
System.out.println(arr[8]);
} catch(ArrayIndexOutOfBoundsException aiobe) {
Exception e = new Exception();
throw(Exception); // throwing the exception to be caught by catch block in main()
e.initCause(aiobe); // supplies the cause to getCause()
}
}
} 출력
Cause = java.lang.ArrayIndexOutOfBoundsException: 8