Checked Exceptions
- เกิดขึ้นตอนคอมไพล์ (Compile-time)
- Compiler ตรวจสอบบังคับ
- จัดการได้ตอนคอมไพล์ (try-catch หรือ throws)
- เป็น subclass ของ Exception (แต่ไม่ใช่ RuntimeException)
- JVM บังคับให้ catch/hande
- ตัวอย่าง:
FileNotFoundException,IOException
Unchecked Exceptions
- เกิดขึ้นตอนรันไทม์ (Runtime)
- Compiler ไม่ตรวจสอบ
- ไม่สามารถ catch/handle ตอนคอมไพล์ได้ (แต่จัดการ runtime ได้)
- เกิดจากข้อผิดพลาดโปรแกรม (logic errors)
- เป็น subclass ของ
RuntimeException - JVM ไม่บังคับให้ catch
- ตัวอย่าง:
NullPointerException,ArrayIndexOutOfBoundsException,NoSuchElementException
สรุป
| คุณสมบัติ | Checked Exception | Unchecked Exception |
|---|---|---|
| เวลาตรวจสอบ | Compile-time | Runtime |
| Compiler ตรวจสอบ | ใช่ | ไม่ |
| บังคับ handle | ใช่ | ไม่ |
| Superclass | Exception | RuntimeException |