public class TryCatchExample {
public static void main(String[] args) {
try {
String str = null;
System.out.println(str.length()); // NullPointerExceptionが発生
} catch (NullPointerException e) {
System.out.println("NPEを捕捉しました。");
} catch (Exception e) {
System.out.println("その他の例外を捕捉しました。");
} finally {
System.out.println("finallyは必ず実行されます。");
}
}
}