Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected exception handling during compilation

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?

like image 522
Pieter Bos Avatar asked Feb 20 '14 12:02

Pieter Bos


People also ask

How do you handle unexpected exceptions in Java?

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.

What is unexpected exception?

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 when an exception object is not caught and handled properly?

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.

What is an exception write any three actions that can be taken after an exception occurs in a program?

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.


1 Answers

It is the feature added in Java 7. Have a look at Rethrowing Exceptions with More Inclusive Type Checking

like image 64
Nandkumar Tekale Avatar answered Oct 13 '22 00:10

Nandkumar Tekale