×
例外処理
try-catch-finally の基本構造と、複数の catch ブロックの例です。
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は必ず実行されます。");
        }
    }
}

もう少し詳しく