I encountered the following method, which to my surprise compiled just fine:
private String getControlMessageBlocking() throws ProtocolException,
InterruptedException, IOException {
try {
// <Code that may throw any of the three listed exceptions>
return controlMessage;
} catch (Exception e) {
throw e;
}
}
Why isn't it necessary for the Exception
to be caught?
The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.
An UnexpectedException is thrown if the client of a remote method call receives, as a result of the call, a checked exception that is not among the checked exception types declared in the throws clause of the method in the remote interface.
What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console.
Catching ExceptionsEvery try block should be immediately followed either by a catch block or finally block. A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in protected code, the catch block (or blocks) that follows the try is checked.
It is the feature added in Java 7. Have a look at Rethrowing Exceptions with More Inclusive Type Checking
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With