PersesTitan(페르) 기술블로그

[Java] InvocationTargetException 본문

Language/Java

[Java] InvocationTargetException

PersesTitan(페르) 2023. 3. 12. 11:09

InvocationTargetException는 생성자 또는 메서드를 호출하였을때 throw를 받는 Exception입니다.

발생하는 예시

  • 생성자를 호출하였을때 throw가 발생하였을때
  • 메소드를 호출하였을때 throw가 발생하였을때
  • 기타

코드

발생하는 예시

(...)
public Main() throws FileNotFoundException {
    new FileReader("");
}
(...)
(...)
try {
    Class.forName("Main").getConstructor().newInstance()
} catch (InvocationTargetException e) {
            e.printStackTrace();
}
(...)

출력

java.lang.reflect.InvocationTargetException
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
Caused by: java.io.FileNotFoundException:  (No such file or directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
    at java.base/java.io.FileReader.<init>(FileReader.java:60)

오라클 링크

https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/InvocationTargetException.html