package org.silk.test;
/**
* 通过测试可以发现,程序会先执行try语句,当有异常的时候,执行catch语句,在try或者catch语句里面
* 如果遇到return或者throw xxxxExceptin语句,则会查找程序是否有finally语句,如果有则执行finally语句,然后返回try-catch块,
* 用try-catch中的return结束方法,如果finally中有return语句,则直接在此return语句里结束方法.
* finally语句总是被执行的,除非在try或者catch语句里面加入System.exit(0)方法;
* @author silk
*
*/
public class Test_Try_Catch_Finally {
public static void main(String[] args) {
System.out.println(get());
}
public static String get(){
try {
System.out.println("do try");
String str=null;
str.charAt(10);
} catch (Exception e) {
// TODO: handle exception
System.out.println("do exception");
return "it is exception ";
}finally{
System.out.println("do finally");
return "it is finally";
}
// return "it is defalut";
}
}
/**
* 通过测试可以发现,程序会先执行try语句,当有异常的时候,执行catch语句,在try或者catch语句里面
* 如果遇到return或者throw xxxxExceptin语句,则会查找程序是否有finally语句,如果有则执行finally语句,然后返回try-catch块,
* 用try-catch中的return结束方法,如果finally中有return语句,则直接在此return语句里结束方法.
* finally语句总是被执行的,除非在try或者catch语句里面加入System.exit(0)方法;
* @author silk
*
*/
public class Test_Try_Catch_Finally {
public static void main(String[] args) {
System.out.println(get());
}
public static String get(){
try {
System.out.println("do try");
String str=null;
str.charAt(10);
} catch (Exception e) {
// TODO: handle exception
System.out.println("do exception");
return "it is exception ";
}finally{
System.out.println("do finally");
return "it is finally";
}
// return "it is defalut";
}
}
2117

被折叠的 条评论
为什么被折叠?



