Back to list
🚀 私の自動化への旅 第 32 日目 – エンションハンドリング
🚀 Day 32 of My Automation Journey – Exception Handling
Translated: 2026/4/17 10:01:05
Japanese Translation
例外処理 🔥
👉 ランタイムエラーの処理
本物のアプリケーションを書き上げるには必須の概念 🚀
👉 エンションとは、プログラムの実行中に起こるエラー
💡 プログラムを崩壊させるのではなく、エレガントに処理する
例外処理がない場合:
例外処理がある場合:
例外処理を学びながら、これらのクイック修了ノートを作成しました。概念と階層を明確に理解するためです。
例外処理とは、ラスタムエンションを取り扱うために Java に実装されたメカニズムであり、アプリケーションの通常のフローを維持します。
👉 処理がない場合 → プログラムがクラッシュする
エンションとは、予期せぬイベントであり:
✔ ランタイムに発生する
Throwable
|
|------ エラ
|
|------ エンション
|
|------ チェック済みエンション
|
|------ チェックされていないエンション (RuntimeException)
👉 Throwable はすべてのエンションとエラーの親クラス
エラーとは、アプリケーションにおける重大な問題を表します
❌ コードで処理することはできません
OutOfMemoryError
StackOverflowError
💡 これは通常のプログラムで処理することを目的としていません
エンションは管理可能な問題です
try-catch
👉 コンパイラムタイムエンションと呼ばれる
✔ コンパイラ(JDK)によって検出される
IOException
FileNotFoundException
SQLException
ClassNotFoundException
セミコロの欠如
誤ったデータ型割り当て
ファイル処理の問題
👉 ランタイムエンションと呼ばれる
✔ 実行中に発生する
NullPointerException
ArithmeticException
ArrayIndexOutOfBoundsException
機能
チェック済みエンション
チェックされていないエンション
検出
コンパイラムタイム
ランタイム
処理
必須
オプション
例
IOException
NullPointerException
✔ エンション = ランタイムの問題
👉 エンションを理解することはエラーの処理だけでなく、安定かつ信頼性の高いプログラムを書くことである 🚀
今日学びました:
エラーとエンションの違い
コンパイラムタイムとランタイムの問題の違い
例外階層の重要性
public class NullPointerDemo {
public static void main(String[] args) {
String name = null;
System.out.println(name.length());
}
}
Exception in thread "main" java.lang.NullPointerException
👉 参照するオブジェクトが null であるため、メソッドにアクセスしようとしました
public class ArithmeticDemo {
public static void main(String[] args) {
int a = 10;
int b = 0;
System.out.println(a / b);
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
👉 0 で割ることは許可されていません
public class ArrayIndexDemo {
public static void main(String[] args) {
int[] arr = {10, 20, 30};
System.out.println(arr[5]);
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
👉 アレイサイズを超えてインデックスにアクセスしました
public class NegativeArrayDemo {
public static void main(String[] args) {
int[] arr = new int[-5];
}
}
Exception in thread "main" java.lang.NegativeArraySizeException
👉 アレイサイズを負数にすることはできません
public class TryCatchDemo {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
System.out.println(a / b);
} catch (ArithmeticException e) {
System.out.println("Handled Arithmetic Exception");
}
System.out.println("Program continues...");
}
}
Handled Arithmetic Exception
Program continues...
👉 エンションがキャッチされ、処理された
public class MultipleCatchDemo {
public static void main(String[] args) {
try {
int[] arr = new int[3];
System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Exception handled");
} catch (Exception e) {
System.out.println("General Exception handled");
}
}
}
Array Index Exception handled
👉 特定のエンションが先に処理される
public class MultiErrorDemo {
public static void main(String[] args) {
try {
String s = null;
System.out.println(s.length());
} catch (NullPointerException e) {
System.
Original Content
Exception Handling 🔥
👉 Handling runtime errors
This is a must-know concept for writing real-world applications 🚀
👉 An exception is an error that occurs during program execution
💡 Instead of crashing the program, we can handle it gracefully
Without exception handling:
With exception handling:
While learning Exception Handling, I created these quick revision notes to clearly understand the concepts and hierarchy.
Exception handling is a mechanism in Java to handle runtime exceptions and maintain the normal flow of the application.
👉 Without handling → program crashes
An exception is an unexpected event that:
✔ Occurs during runtime
Throwable
|
|------ Error
|
|------ Exception
|
|------ Checked Exception
|
|------ Unchecked Exception (RuntimeException)
👉 Throwable is the parent class of all errors and exceptions
Errors represent serious problems in the application
❌ Cannot be handled using code
OutOfMemoryError
StackOverflowError
💡 These are not meant to be handled in normal programs
Exceptions are manageable problems
try-catch
👉 Also known as compile-time exceptions
✔ Detected by compiler (JDK)
IOException
FileNotFoundException
SQLException
ClassNotFoundException
Missing semicolon
Wrong datatype assignment
File handling issues
👉 Also known as runtime exceptions
✔ Occur during execution
NullPointerException
ArithmeticException
ArrayIndexOutOfBoundsException
Feature
Checked Exception
Unchecked Exception
Detected
Compile time
Runtime
Handling
Mandatory
Optional
Example
IOException
NullPointerException
✔ Exception = runtime issue
👉 Understanding exceptions is not just about handling errors — it’s about writing stable and reliable programs 🚀
Today I understood:
Difference between error & exception
Compile-time vs runtime issues
Importance of exception hierarchy
public class NullPointerDemo {
public static void main(String[] args) {
String name = null;
System.out.println(name.length());
}
}
Exception in thread "main" java.lang.NullPointerException
👉 Trying to access method on null object
public class ArithmeticDemo {
public static void main(String[] args) {
int a = 10;
int b = 0;
System.out.println(a / b);
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
👉 Division by zero is not allowed
public class ArrayIndexDemo {
public static void main(String[] args) {
int[] arr = {10, 20, 30};
System.out.println(arr[5]);
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
👉 Accessing index outside array size
public class NegativeArrayDemo {
public static void main(String[] args) {
int[] arr = new int[-5];
}
}
Exception in thread "main" java.lang.NegativeArraySizeException
👉 Array size cannot be negative
public class TryCatchDemo {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
System.out.println(a / b);
} catch (ArithmeticException e) {
System.out.println("Handled Arithmetic Exception");
}
System.out.println("Program continues...");
}
}
Handled Arithmetic Exception
Program continues...
👉 Exception is caught and handled
public class MultipleCatchDemo {
public static void main(String[] args) {
try {
int[] arr = new int[3];
System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Exception handled");
} catch (Exception e) {
System.out.println("General Exception handled");
}
}
}
Array Index Exception handled
👉 Specific exception handled first
public class MultiErrorDemo {
public static void main(String[] args) {
try {
String s = null;
System.out.println(s.length());
} catch (NullPointerException e) {
System.out.println("Null Pointer handled");
} catch (Exception e) {
System.out.println("General Exception handled");
}
}
}
Null Pointer handled
👉 Yes, we can write another try after catch
public class NestedTryDemo {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("First exception handled");
}
try {
String s = null;
System.out.println(s.length());
} catch (NullPointerException e) {
System.out.println("Second exception handled");
}
}
}
First exception handled
Second exception handled
👉 After one try-catch, we can write another
✔ Only one exception occurs at a time
Exception class should be last
✔ Exception = runtime error
👉 Exception handling makes your program strong, safe, and production-ready 🚀
Stay tuned 🚀