If i write things like this :

public static void main(String[] args) {
    try {
    } catch (MalformedURLException e) { 
        e.printStackTrace()
    };
}

Java compiler shows compilation error at catch clause of MalformedURLException. If i insert line URL url = new URL("HI"); in try block , complaining stops. I thought java must be binding these checked exceptions with package. Then i tried another class of java.net package by inserting " CookieManager manager = new CookieManager(); " only in try block. Compilation error again starts.

So how does JVM bind these checked exceptions with java classes for compilation time errors like this?

有帮助吗?

解决方案

A method declaration specifies which exceptions the method throws. This information is accessible to the compiler. The compiler emits an error message if you attempt to catch an exception that cannot be thrown by the code invoked in the try clause.

其他提示

In case of Checked Exceptions something must be thrown before it is caught!

public static void main(String[] args) {
    try {
    } catch (MalformedURLException e) {
        e.printStackTrace()
    };
}

In this these is no possibility of throwing an Exception so you cannot catch it.

When you add

URL url = new URL("HI");

the constructor throws the Exception and hence you need to catch it.

public URL(String protocol, String host, int port, String file)
    throws MalformedURLException
{
    this(protocol, host, port, file, null);
}

But the CookieManager constructor does not throw any Exception. So no need to catch it.

/**
 * Create a new cookie manager.
 *
 * <p>This constructor will create new cookie manager with default
 * cookie store and accept policy. The effect is same as
 * <tt>CookieManager(null, null)</tt>.
 */
public CookieManager() {
    this(null, null);
}

For each method the checked exceptions are saved into the class file.

When you call such a method with a checked exception the compiler checks that you either catch the exception or throw it from your method.

From http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21:

14.21. Unreachable Statements

It is a compile-time error if a statement cannot be executed because it is unreachable.

...

A catch block C is reachable iff both of the following are true:

  • Either the type of C's parameter is an unchecked exception type or Throwable; or some expression or throw statement in the try block is reachable and can throw a checked exception whose type is assignable to the parameter of the catch clause C.

An expression is reachable iff the innermost statement containing it is reachable.

See §15.6 for normal and abrupt completion of expressions.

  • There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.

The first condition is not true if the try block is empty: there is nothing that "can throw a checked exception whose type is assignable to the parameter of the catch clause C".

This is just Java's super vigilant compiler complaining that: if you are not throwing any exception, You are not calling any code that throws it, then why you have a useless try-catch block sitting around wasting space ?

So, when you give compiler a reason to believe that an exception might occur in this block, it shuts up.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top