Exception Handling:
Exception handling is a mechanism in Java that deals with runtime errors, allowing the program to continue its normal flow instead of crashing.
An exception is an unwanted or unexpected event that occurs during the execution of a program, disrupting its normal flow.
Keypoints:
- Exceptions are problems that occur during execution, not syntax errors.
- When an exception occurs, Java throws the exception object.
- If not handled, the program terminates abnormally.
- Exception handling helps catch and deal with these problems.
Exception:
An exception in Java is an event that disrupts the normal flow of a program’s execution. It usually occurs due to errors or unexpected situations at runtime, such as:
- Division by zero
- Accessing a null object
- File not found
- Invalid input from the user When an exception occurs, the normal flow of the program is interrupted, and Java creates an exception object that contains information about the errors.
Error:
An error in Java is a serious problem that arises during the execution of a program and typically cannot be handled or recovered from by the program itself.
Errors are usually caused by issues outside the application's control and indicate problems with the environment in which the program is running.
Examples of error:
- OutOfMemoryError->Happens when the JVM runs out of memory.
- StackOverflowError->Happens due to infinite recursion or excessive memory use in the call stack.
- VirtualMachineError->Issues with the JVM itself.
Types of Exceptions:
Checked Exception:
Definition:
Checked exceptions are exceptions that are checked at compile-time.
The compiler ensures that these exceptions are either caught using try-catch or declared using the throws keyword in the method signature.
Key Features:
- The program must handle them explicitly.
- They typically occur due to external conditions, like file I/O errors or database problems.
- If you don't handle them, the compiler will give an error.
Example:
IOException->Input/output errors->Reading from or writing to a file that doesn't exist.
FileNotFoundException->File cannot be found->Opening a file path that does not exist.
Unchecked Exceptions (Runtime Exceptions):
Definition:
Unchecked exceptions are exceptions that are checked at runtime, not at compile-time. The compiler does not require you to handle them explicitly.
Key Features:
- They are usually programming bugs or logic errors.
- Handling them is optional, but ignoring them can cause program crashes.
- They are subclasses of RuntimeException.
Example:
ArithmeticException->Invalid arithmetic operation->Dividing by zero.
NullPointerException->Accessing a null reference->Calling a method on a null object.
Throwable Class:
Root class for all exceptions and errors.
It can be thrown or caught.
Provides methods like:
- getMessage()
- printStackTrace()
- getCause()
Some basic keywords in Exceptions Handling:
try – Block where you write code that might throw an exception.
catch – Block that handles the exception.
finally – Block that executes after try-catch, whether an exception occurred or not.
throw – Used to explicitly throw an exception.
throws – Declares exceptions that a method might throw.
Top comments (0)