Java จัดการข้อผิดพลาดด้วย try/catch/finally และแยกอย่างเฉพาะเจาะจง checked exceptions (ต้องประกาศหรือจัดการ) จาก unchecked exceptions (runtime ไม่บังคับ) การเข้าใจความแตกต่างนี้เป็นสิ่งสำคัญต่อการจัดการข้อผิดพลาดของ Java
Java จัดการข้อผิดพลาดด้วย try/catch/finally และแยกอย่างเฉพาะเจาะจง checked exceptions (ต้องประกาศหรือจัดการ) จาก unchecked exceptions (runtime ไม่บังคับ) การเข้าใจความแตกต่างนี้เป็นสิ่งสำคัญต่อการจัดการข้อผิดพลาดของ Java
try {
FileReader f = new FileReader("file.txt"); // may throw IOException
// ... use it ...
} catch (FileNotFoundException e) {
System.out.println("not found: " + e.getMessage()); // specific first
} catch (IOException e) {
System.out.println("io error"); // broader after
} finally {
System.out.println("always runs — cleanup"); // runs no matter what
}
finally ทำงานเสมอ (ทำความสะอาด) แม้ exception ถูกโยนหรือ return เกิดขึ้น
Throwable
├── Error — serious JVM problems (OutOfMemoryError) — don't catch
└── Exception
├── RuntimeException → UNCHECKED (not enforced by the compiler)
│ NullPointerException, IllegalArgumentException, IndexOutOfBounds...
└── (other Exceptions) → CHECKED (compiler enforces handling)
IOException, SQLException...
// MUST either catch it or declare `throws` — the compiler won't let you ignore it
public void readFile() throws IOException { // declare it
Files.readAllLines(Paths.get("file.txt")); // throws checked IOException
}
Checked exceptions (IOException, SQLException) แทนสภาวะที่สามารถกู้คืนได้คาดหวัง (ไฟล์หายไป ความล้มเหลวของเครือข่าย) Compiler บังคับ ให้คุณจัดการหรือประกาศ — ทำให้การจัดการความล้มเหลวภายนอกชัดแจ้ง
// NOT required to declare or catch — usually indicate PROGRAMMING BUGS
String s = null;
s.length(); // NullPointerException (unchecked)
int x = arr[10]; // ArrayIndexOutOfBoundsException (unchecked)
Integer.parseInt("abc"); // NumberFormatException (unchecked)
Unchecked (RuntimeException subclasses) โดยปกติแสดงถึงข้อผิดพลาดของการเขียนโปรแกรม — คุณแก้ bug แทนที่จะจับมันทุกที่ Compiler ไม่บังคับให้จัดการ
// resources implementing AutoCloseable are closed automatically
try (FileReader f = new FileReader("file.txt")) {
// use f
} // f.close() called automatically, even on exception — no finally needed
✓ Catch specific exceptions, not bare Exception
✓ Use try-with-resources for files/connections (auto-close)
✓ Don't swallow exceptions silently (empty catch blocks)
✓ Throw meaningful exceptions; include context in messages
✓ Custom exceptions extend Exception (checked) or RuntimeException (unchecked)
การจัดการข้อผิดพลาดที่แข็งแรงเป็นสิ่งจำเป็น และ ความแตกต่าง checked/unchecked ของ Java เป็นคุณลักษณะที่กำหนด (และเป็นที่ถกเถียง) ที่คุ้มค่าที่จะเข้าใจอย่างลึกซึ้ง
Checked exceptions บังคับให้จัดการความล้มเหลวภายนอกที่คาดหวัง (I/O DB) อย่างชัดแจ้ง — ส่งเสริมความเชื่อถือได้ แต่บางครั้งถูกวิจารณ์ว่าปากมาก; unchecked exceptions แทนข้อบกพร่องที่ต้องแก้ไขแทนที่จะจับจริงๆ
การรู้ hierarchy เมื่อแต่ละประเภทนำไป semantics ของ try/catch/finally และ try-with-resources ที่ทันสมัยสำหรับการทำความสะอาดอัตโนมัติเป็นสิ่งพื้นฐานในการเขียน Java ที่ถูกต้องและเชื่อถือได้ — และสำหรับการออกแบบ exceptions ของคุณเองอย่างเหมาะสม
ความแตกต่างนี้หล่อให้ API ของ Java ถูกออกแบบและเป็นหัวข้อสัมภาษณ์และการทบทวนโค้ดที่บ่อย