Answer:
- Robustness: Ensures the application continues running even after encountering errors by providing structured handling of exceptions.
- Error Isolation: Provides a mechanism to separate error-handling code from regular business logic, making the code more modular.
- Maintenance: Improves code readability and maintainability by centralizing error handling in
try-catchblocks. - Custom Exceptions: Enables developers to create application-specific exceptions that describe issues more accurately.
- Controlled Termination: Allows graceful application termination by managing exceptions effectively.
Answer:
- Introduced in Java 7, the
try-with-resourcesstatement is used to automatically close resources like files, sockets, and database connections after thetryblock is executed. - The resources must implement the
AutoCloseableinterface to be compatible with this feature. - This reduces boilerplate code and ensures resources are closed properly, even if an exception occurs.
Example:
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}In this example, the BufferedReader is automatically closed when the try block exits, whether normally or due to an exception.
Answer:
- Yes, starting from Java 7, multiple exceptions can be handled in a single
catchblock using a pipe (|) operator. - This feature reduces redundancy and makes code more concise by avoiding repetitive
catchblocks.
Example:
try {
int[] arr = new int[5];
System.out.println(arr[10]); // ArrayIndexOutOfBoundsException
String str = null;
System.out.println(str.length()); // NullPointerException
} catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
System.out.println("An exception occurred: " + e.getMessage());
}Here, both exceptions are handled in the same block without duplicating code.
Answer:
- Error:
- Represents serious system-level issues that an application cannot reasonably handle (e.g.,
OutOfMemoryError,StackOverflowError). - Errors usually indicate problems with the JVM or hardware and are unchecked.
- Represents serious system-level issues that an application cannot reasonably handle (e.g.,
- Exception:
- Represents conditions that applications might want to catch and handle (e.g.,
IOException,ArithmeticException). - Exceptions can be checked or unchecked.
- Represents conditions that applications might want to catch and handle (e.g.,
Key Difference: Errors are unrecoverable, while exceptions are recoverable through proper handling.
Answer:
IOException:- A general exception that occurs during input/output operations.
- It is the parent class for many specific exceptions, including
FileNotFoundException.
FileNotFoundException:- A more specific exception that occurs when a file cannot be found or opened.
- It is a subclass of
IOException.
Example:
try {
FileReader reader = new FileReader("nonexistent.txt");
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (IOException e) {
System.out.println("I/O exception occurred.");
}Answer:
- Yes, an exception can be rethrown to a higher-level method or caller using the
throwkeyword. - This is useful when the current method cannot fully handle the exception and wants to delegate it.
Example:
public void readFile() throws IOException {
try {
FileReader reader = new FileReader("file.txt");
} catch (IOException e) {
System.out.println("Logging the exception: " + e.getMessage());
throw e; // Rethrowing the exception
}
}Here, the exception is logged and then rethrown for the caller to handle.
Answer:
- When an exception is not caught or handled, it propagates up the call stack.
- If it reaches the top of the stack without being caught, the JVM's default exception handler terminates the program and prints a stack trace.
- Example:
public void divideByZero() {
int result = 10 / 0; // Throws ArithmeticException
}Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at MyClass.divideByZero(MyClass.java:5)
...
Answer:
- A stack trace is a report showing the active method call sequence at the time of an exception.
- It helps developers pinpoint the exact location and cause of errors in the code.
Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
e.printStackTrace(); // Prints the stack trace
}Output:
java.lang.ArithmeticException: / by zero
at MyClass.main(MyClass.java:5)
The stack trace indicates the file name and line number where the error occurred.
Answer:
- As mentioned earlier, multiple exceptions can be caught in a single block using the pipe (
|) operator. - This is especially useful when the handling logic is the same for multiple exceptions.
Example:
try {
String[] data = {"10", "abc"};
int num = Integer.parseInt(data[1]); // NumberFormatException
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
System.out.println("Exception occurred: " + e.getClass().getSimpleName());
}Answer:
- A suppressed exception occurs during the closing of a resource in a
try-with-resourcesblock when another exception is already thrown. - These exceptions are suppressed and can be retrieved using the
getSuppressed()method.
Example:
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
throw new IOException("Primary exception");
} catch (IOException e) {
for (Throwable suppressed : e.getSuppressed()) {
System.out.println("Suppressed: " + suppressed);
}
}Here, any exceptions that occur while closing the BufferedReader are suppressed and can be accessed for debugging purposes.