`

Java Exception | 异常

阅读更多
  
http://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html

Throwable Class and Its Subclasses:

Java中方法的调用构成方法调用栈,方法调用栈中方法的顺序与方法的被调用顺序是相反的。
Java中程序的任何一处抛出Throwable对象后,都是采用按方法调用栈逐级上溯(即与方法调用顺序相反的顺序逐级上溯)的机制来搜寻可以处理被抛出的Throwable对象的方法(注意不管是Checked Exceptions 还是 Unckecked Exceptions,都是使用这种机制处理的)。如果找到了,则异常对象在该方法中被处理;如果搜寻所有方法直至程序入口都未能找到一个可以处理该Throwable对象的方法,则程序退出。如下面两个图所示:

The call stack.

Searching the call stack for the exception handler.




Checked Exceptions & Unckecked Exceptions:
Checked Exceptions:
any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.
checked means they will be checked at compiletime it self,so Checked exceptions must be explicitly caught or propagated(只有两条路:使用try-catch-finally捕获它,或者在方法声明中throws它)。
Unckecked Exceptions:
RuntimeException, Error, and their subclasses
unchecked means you need not handle the unchecked exceptions and they will be handled by the JVM.


什么时候使用checked exception,什么时候使用unchecked exception?
Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

对checked exception,什么时候应该抛出,什么时候应该捕获?Catch or Propagate Exceptions?
You might be wondering whether you should catch or propate exceptions thrown in your program. It depends on the situation. In many applications you can't really do much about the exception but tell the user that the requested action failed. In these applications you can usually catch all or most exceptions centrally in one of the first methods in the call stack. You may still have to deal with the exception while propagating it though (using finally clauses). For instance, if an error occurs in the database connection in a web application, you may still have to close the database connection in a finally clause, even if you can't do anything else than tell the user that the action failed. How you end up handling exceptions also depends on whether you choose checked or unchecked exceptions for your application. There is more on that in other texts in the error handling trail.



throws和throw的区别
引用
throw用来在语句中抛出一个异常,而throws用在方法声明中,表示这个方法会抛出某类异常。
throw语法:throw <异常对象>
在方法声明中,添加throws子句表示该方法将抛出异常。
throws语法:[<修饰符>]<返回值类型><方法名>([<参数列表>])[throws<异常类>]
其中:异常类可以声明多个,用逗号分割。
区别:
throws可以单独使用;
而对throw:
如果语句中throw抛出的是checked exception,则该checked exception必须被捕获(try-catch[-finally])或者抛出(方法声明中throws);所以可以认为,使用throw抛出checked exception的时候throw不可以单独使用,必须和try-catch[-finally] 或 throws配合使用
如果语句中throw抛出的是unchecked exception(RuntimeException,Error,及他们的子类),unchecked exception是不需要你在代码中做处理的;可以认为,使用throw抛出unchecked exception的时候throw可以单独使用
在方法的一个分支中(主干分支或任意一个条件分支,条件分支如if分支、else分支),throw一个Throwable对象,则如果该分支中throw子句后还存在代码块,则这些代码块会被认为是Unreachable,无法编译通过。
    //存在三个分支:main主分支、if分支、else分支
    public static void main( String[] args ) throws Throwable {
        if(true) {
            throw new Throwable("err");
            //System.out.println("ddd"); //不可达
        } else {
            System.out.println(""); //可达
        }
        throw new NullPointerException("null err"); //可达
        //m1(); //不可达
    }



Sources:
Checked or Unchecked Exceptions?
http://tutorials.jenkov.com/java-exception-handling/checked-or-unchecked-exceptions.html
Basic try-catch-finally Exception Handling in Java
http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html
  • 大小: 7.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics