首页 > 代码库 > 5.27 java中try,catch,throw,throws,finally

5.27 java中try,catch,throw,throws,finally

http://blog.sina.com.cn/s/blog_4f1c99de0100tyty.html

 

try{
//代码区
}catch(Exception e){
//异常处理
}
代码区如果有错误,就会返回所写异常的处理。

 

首先要清楚,如果没有try的话,出现异常会导致程序崩溃。
而try则可以保证程序的正常运行下去。

Java的异常处理是通过5个关键字来实现的:try,catch,throw,throws,finally。JB的在线帮助中对这几个关键字是这样解释的:
Throws:  Lists the exceptions a method could throw.
Throw:   Transfers control of the method to the exception handler.
Try:    Opening exception-handling statement.
Catch:  Captures the exception.
Finally: Runs its code before terminating the program.