자바에서 예외처리할때 왜 throw null;이 가능한거죠?
조회수 3069회
소스코드
public class WhatTheShoot {
public static void main(String args[]){
try {
throw null;
} catch (Exception e){
System.out.println(e instanceof NullPointerException);
System.out.println(e instanceof FileNotFoundException);
}
}
}
위 코드를 런하면
true
false
라는 결과가 나오는데요. 컴파일에러가 날줄 알았는데 잘 컴파일이 되서 신기했어요. 왜 자바에서는 null을 예외로 던질수가 있는거죠? 그리고 왜 그게 NullPointerException으로 캐스팅이되는거죠?
댓글 입력