Exceptions

Error indicates serious problems that a reasonable application should not try to catch.

  • StackOverflowError

Exception indicates conditions that a reasonable application might want to catch.

Error and Exception extend from Throwable

Error and all exceptions in Runtime Exception can be not caught in the compiling.

ExceptionThrowable的子类,包含一些有用的函数。

Catch an exception

try {
    return;
} catch (IOException e) {
    return;
} catch (Exception e) {
    return;
} finally {
    return;
}

Define an exception

ExceptionThrowable的子类,包含一些有用的函数。

public class MyException extends Exception {
    public MyException(){
        super();
    }
    public MyException(String msg){
        super(msg);
    }
}

New feature in Java 7

try (BufferedReader br = new new BufferedReader(new FileReader(path))) {
    return br.readline();
}

old style:

BufferedReader br = new BufferedReader(new FileReader(path));
try {
    return br.readline();
} finally {
    if (br != null) br.close();
}

Throw vs. Throws

  1. throw instance (throwable)
  2. throws

throws is a keyword in Java, which is used in the signature of method to indicate that this method might throw one of the listed type exceptions.

  • re-throw
  • multiple catches

results matching ""

    No results matching ""