Exception Handling
Don't let errors crash your app. Learn about Exceptions, Errors, and the hierarchy.
Don't let errors crash your app. Learn about Exceptions, Errors, and the hierarchy. This interview-focused guide covers essential exception handling concepts for technical interviews.
Exception Handling
An Exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.
Error vs Exception
- Error: Serious problems that a reasonable application should not try to catch (e.g.,
OutOfMemoryError,StackOverflowError). Usually caused by the environment. - Exception: Conditions that a reasonable application might try to catch (e.g.,
NullPointerException,FileNotFoundException).
Types of Exceptions
1. Checked Exceptions (Compile-time)
The compiler checks these at compile time. You must handle them (try-catch or throws).
- Examples:
IOException,SQLException.
2. Unchecked Exceptions (Runtime)
The compiler does not check these. They happen due to programming errors.
- Examples:
ArithmeticException,NullPointerException,IndexOutOfBoundsException.
Interactive Code
Trigger an exception!
AI Mentor
Confused about "Java exception hierarchy: Checking vs Unchecked exceptions"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 3Which type of exception MUST be handled exclusively by the programmer?
Next Steps
Now let's learn how to actually handle these exceptions with try-catch.