Checked exceptions
The exception which are checked by compiler for smooth execution of the program are called checked exceptions.
Example-:
Example-:
1. IOException : This exception is thrown when an input or output operation (e.g., reading or writing to a file) encounters an issue, such as a file not being found or permission denied.
2. SQLException : It is thrown when a database operation fails, such as trying to execute an invalid SQL query or losing a connection to the database.
3. ClassNotFoundException : Occurs when the JVM cannot find a class at runtime that is required by the program.
4. InterruptedException : Raised when a thread is interrupted while it's waiting, sleeping, or in a blocked state.
Real life Example-:
Hall ticket missing exception
pen not working exceptionfile not found exception etc.
In our program if there is chance of raising checked exception then compulsory we should handle that checked exception (either by try-catch or by throws key word) otherwise we will get compile time error.
Here's an example of how to handle a checked exception using a try-catch block:
try {
// Code that may throw a checked exception
// ...
} catch (IOException e) {
// Handle the IOException
// ...
}
Or you can declare that your method may throw a checked exception using the `throws` keyword:
public void readFile() throws IOException {
// Code that may throw an IOException
// ...
}
By explicitly handling or declaring checked exceptions, Java enforces error handling and ensures that exceptions are dealt with appropriately, which can lead to more robust and reliable code.
Unchecked exceptions
The exception which are not checked by compiler whether programmer handling are not such type of exception are called unchecked exceptions
Common examples of unchecked exceptions in programming languages like Java include:
1. NullPointerException: Occurs when an attempt is made to access or manipulate an object that is null (i.e., it doesn't reference any memory location).
2. ArrayIndexOutOfBoundsException: Occurs when an attempt is made to access an array element using an index that is outside the valid range.
3. ArithmeticException: Occurs when mathematical operations result in an undefined or impossible value, such as dividing by zero.
4. IllegalArgumentException: Occurs when a method is called with an argument that has an illegal or inappropriate value.
5. ClassCastException: Occurs when an attempt is made to cast an object to a class that is not compatible with its actual type.
6. UnsupportedOperationException: Occurs when an operation is not supported or implemented by an object or data structure.
Unchecked exceptions are often caused by errors in the program's logic or incorrect usage of APIs, and they indicate a problem that should be fixed in the code. While you are not required to explicitly catch or declare unchecked exceptions, it's a good practice to handle them gracefully to prevent unexpected program crashes and improve the reliability of your software.
Note -:Run type exception and its child classes, error and its child classes are unchecked, except these remaining are checked.
Fully checked exception-:
A checked exception said to be fully check if and only if all its child class also checked.
Example-: IOException ,Interrupted Exception
Patrial checked exception-:
A checked exception, said to be partially if and only if some of its child classes are unchecked exception.
Example-: Exception ,throwable.
The only possible partially checked exception in Java are.
- Exception
- Throwable
IOException -----> checked Exception(fully)
RunTime Exception---->Unchecked
InterruptedException ---->Checked (fully)
Error ----->Unchecked
Throwable ------>Checked(partially)
ArithmeticException ------>Unchecked
Exception -----> checked(partially)
NullPointerException ----->unchecked
FileNotFoundException ---->checked(fully)