Handle exceptions using try/catch/finally, try-with-resources, and multi-catch blocks, including custom exceptions.
1. The correct answer is B.
Explanation:
java.lang.RuntimeException class.
java.lang.RuntimeException. Checked exceptions are subclasses of java.lang.Exception but not of java.lang.RuntimeException.throws keyword.
try-catch block or declared in the method signature with the throws keyword. This is to ensure that the exception is properly handled at some point in the code.2. The correct answer is A.
Explanation:
Exception. The methodThatThrowsException method throws this custom exception, which is then caught and handled in the main method.Exception, not RuntimeException, making it a checked exception rather than an unchecked one.methodThatThrowsException, so it will compile without issues.catch block.3. The correct answer is B.
Explanation:
1
catch block returns 1, the finally block will override this return value with 2.2
finally block always executes and its return value overrides the return value from the catch block, resulting in 2 being printed.RuntimeException is thrown in the try block, it is caught by the catch block, and no exception propagates to cause a runtime error.4. The correct answer is D.
Explanation:
try block can be followed by a finally block without a catch block.catch block.
catch block is valid, it is not necessary for the code to compile. The try block can be used with only a finally block.finally block.
finally block is not necessary for the code to compile. The code is valid with the finally block present.try block must be followed by either a catch block, a finally block, or both.5. The correct answers are C and D.
Explanation:
try-with-resources, the catch block is required.
try-with-resources statement, the catch block is optional. The primary purpose of try-with-resources is to ensure that each resource is closed at the end of the statement, whether an exception is thrown or not.throws keyword is used to throw an exception.
throws keyword is used in method declarations to specify that the method can throw an exception, not to throw an exception. The throw keyword is used to actually throw an exception.try-with-resources block, if you declare more than one resource, they have to be separated by a semicolon.
try-with-resources block, if you declare more than one resource, they must be separated by a semicolon.catch block is defined for an exception that couldn’t be thrown by the code in the try block, a compile-time error is generated.
catch block is defined for an exception that cannot be thrown by the code in the try block, the compiler will generate an error because the catch block is unreachable.6. The correct answer is E.
Explanation:
Close Exception
IOException from close() will occur, it will be suppressed by the RuntimeException.RuntimeException
RuntimeException, but it will not print its message directly because the catch block does not handle it.RuntimeException and then CloseException
RuntimeException is primary, and the IOException is suppressed. Both messages are not printed in sequence.RuntimeException thrown in the try block is not caught by the catch (IOException e) block. Hence, the stack trace of the RuntimeException is printed.7. The correct answers are B and C.
Explanation:
A. java.io.FileNotFoundException is incorrect. It is a subclass of java.io.IOException, which in turn is a subclass of java.lang.Exception, making it a checked exception.
B. java.lang.ArithmeticException is correct. It is a direct subclass of java.lang.RuntimeException and represents arithmetic errors such as division by zero.
C. java.lang.ClassCastException is correct. It is a direct subclass of java.lang.RuntimeException and indicates an invalid cast operation.
D. java.lang.InterruptedException is incorrect. It is a direct subclass of java.lang.Exception, making it a checked exception. It indicates that a thread has been interrupted.
8. The correct answer is D.
Explanation:
"Try Block Exception" is printed.
Try Block Exception is the main exception and is not directly printed because the catch block checks for suppressed exceptions first."Close Exception" is printed.
Close Exception is not directly printed; it is suppressed and accessed via the getSuppressed method."Try Block Exception" and "Close Exception" are printed.
"Suppressed: Close Exception" is printed.
RuntimeException thrown in the try block is the main exception, and the RuntimeException from the close method is suppressed. The catch block prints the suppressed exception message, "Suppressed: Close Exception".Do you like what you read? Would you consider?
Do you have a problem or something to say?