An exception is an event that disrupts normal program execution. Java handles exceptions using:
trycatchfinallythrowthrows
Exceptions are objects that inherit from the Throwable class.
Throwable is the root of Java's exception hierarchy.
Two main subclasses:
- Exception – recoverable issues
- Error – serious system-level problems (not recoverable)
Throwable
├── Exception
└── Error
| Aspect | Exception | Error |
|---|---|---|
| Meaning | Problems caused by program or external conditions | Serious issues in JVM/system |
| Recoverable? | Yes, usually | No |
| Handled with try-catch? | Yes | Not recommended |
| Examples | NullPointerException, IOException | OutOfMemoryError, StackOverflowError |
Exceptions are mainly categorized into:
- Checked Exceptions
- Unchecked Exceptions
- Errors
Checked exceptions must be handled or declared using throws.
Examples:
Occurs during file operations.
FileReader fr = new FileReader("abc.txt");Database access problems.
Class not found during runtime loading.
File missing.
Thread interruptions.
Error while parsing formatted text.
Checked exceptions represent external failures that the program should handle gracefully.
Subclass of RuntimeException.
Do not need to be declared or handled.
Examples:
String s = null;
s.length();int x = 10 / 0;int[] arr = new int[5];
arr[10] = 2;int x = Integer.parseInt("abc");Object o = new Integer(5);
String s = (String) o;Bad input to method.
Method called at wrong time.
Unchecked exceptions usually represent programming errors.
Errors occur in the JVM or system. You should NOT handle them with try-catch.
Examples:
new int[1000000000];void test() { test(); }JVM internal issue.
Class missing at runtime.
Assertion failure.
Errors indicate serious problems the application cannot fix.
Throwable
│
├── Exception (Recoverable)
│ ├── RuntimeException (Unchecked)
│ │ ├── NullPointerException
│ │ ├── ArithmeticException
│ │ ├── ArrayIndexOutOfBoundsException
│ │ ├── ClassCastException
│ │ ├── NumberFormatException
│ │ └── IllegalArgumentException
│ │
│ ├── IOException (Checked)
│ │ ├── FileNotFoundException
│ │ └── EOFException
│ │
│ ├── SQLException (Checked)
│ ├── ClassNotFoundException (Checked)
│ ├── InterruptedException (Checked)
│ └── InvocationTargetException (Checked)
│
└── Error (Not Recoverable)
├── OutOfMemoryError
├── StackOverflowError
├── InternalError
├── AssertionError
└── NoClassDefFoundError
Accessing a method or property on a null reference.
Division by zero or illegal arithmetic operation.
Index outside array boundary.
Invalid type casting.
String → number conversion fails.
Invalid argument to a method.
I/O failures → external resource issues.
| Category | Checked Exception | Unchecked Exception | Error |
|---|---|---|---|
| Parent | Exception | RuntimeException | Error |
| Checked at compile time? | Yes | No | No |
| Must handle using try/throws | Yes | No | No |
| Recoverable? | Usually | Sometimes | No |
| Example | IOException | NullPointerException | OutOfMemoryError |
-
Throwable is the root of all exceptions and errors.
-
Exception = problems you should handle.
-
RuntimeException = programming mistakes.
-
Error = serious system failures → not handled.
-
Checked exceptions → must be handled.
-
Unchecked exceptions → runtime failures.