English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
아니요, 어떤 예외도 명시적으로 발생시키기 위해, 해당 예외의 객체를 생성하고 throw 키워드를 사용하여 발생시키기 위해 필요합니다.
객체를 생성하지 않으면 예외를 명시적으로 발생시키기 어렵습니다. 대신, 해당 예외를 일으키는 계획을 생성할 수 있습니다.
다음 Java 프로그램은 NullPointerException을 일으킵니다
public class ExceptionExample { public static void main(String[] args) { System.out.println("Hello"); NullPointerException nullPointer = new NullPointerException(); throw nullPointer; } }
출력 결과
Hello Exception in thread "main" java.lang.NullPointerException at MyPackage.ExceptionExample.main(ExceptionExample.java:6)