In Java, exceptions are categorized into checked and unchecked exceptions to handle errors during runtime effectively.
Checked exceptions are exceptions that are checked at compile-time. The compiler ensures that these exceptions are caught or declared in the method signature. They are typically used for recoverable conditions, such as file not found or network-related issues.
// Example for Checked Exception
try {
FileReader fr = new FileReader("test.txt");
} catch (FileNotFoundException e) {
System.out.println("File not found exception caught: " + e.getMessage());
}
Unchecked exceptions are exceptions that are not checked at compile time. They are subclasses of RuntimeException and signify programming errors, such as logic errors or improper use of API. These exceptions do not need to be declared or caught.
// Example for Unchecked Exception
int[] arr = new int[5];
try {
System.out.println(arr[10]); // This will throw ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of bounds exception caught: " + e.getMessage());
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?