Assignment
Name: Zara Khan
Reg#: 22-CP-22
Submitted to: Dr. Naveed Baloch
Course Details: OOP (Theory)
Dated: 6th April, 2024
Task:
Write all possible exceptions available in java and
write code examples to handle those exceptions.
Exceptions:
An exception is generally an unwanted event that interrupts the normal
flow of program.
Exception Handling:
Exception handling in Java is a mechanism used to deal with runtime
errors or exceptional situations that occur during the execution of a
program.
Types of Exceptions:
Java exceptions can be broadly classified into two types:
Checked Exceptions: These are the exceptions that are checked at
compile time. Examples include IOException,
ClassNotFoundException, etc. You need to handle or declare
checked exceptions.
Unchecked Exceptions (Runtime Exceptions): These are the
exceptions that are not checked at compile time. Examples
include NullPointerException, ArithmeticException, etc. You are
not required to handle or declare unchecked exceptions.
Checked Exceptions:
IOException: Thrown for I/O-related errors, such as reading from or
writing to files, network errors, etc.
FileNotFoundException: Thrown when a file specified by its pathname
does not exist.
ClassNotFoundException: Thrown when trying to load a class by its
string name but the class cannot be found in the classpath.
ParseException: Thrown when parsing fails during parsing-related
operations, such as parsing dates or numbers.
InterruptedException: Thrown when a thread is interrupted while it's
sleeping, waiting, or occupied.
NoSuchMethodException: Thrown when a particular method cannot
be found during reflection.
Unchecked Exceptions:
1. ArithmeticException: Thrown when an arithmetic operation fails,
such as division by zero.
2. NullPointerException: Thrown when trying to access or call a
method on an object reference that points to null.
3. ArrayIndexOutOfBoundsException: Thrown when attempting to
access an array element with an invalid index.
4. NumberFormatException: Thrown when attempting to convert a
string to a numeric format, but the string does not contain a valid
numeric value.
5. IllegalArgumentException: Thrown when an illegal or inappropriate
argument is passed to a method.
6. IllegalStateException: Thrown when the state of an object is not as
expected for the method being invoked.
7. ConcurrentModificationException: Thrown when an object is
modified concurrently when it's not permissible.
8. ClassCastException: Thrown when trying to cast an object to a
subclass type which is not compatible.
9. StackOverflowError: Thrown when the stack overflows due to
excessive recursion.
10. OutOfMemoryError: Thrown when the Java Virtual Machine
cannot allocate an object because it is out of memory.