public class ExceptionDemo1 {

     public static void main(String[] args) {
try {
int b = ;
int res = /b;
System.out.println(res);
} catch (Exception e) {
e.printStackTrace();
System.out.println("catch...");
}finally{
System.out.println("finally...");
}
System.out.println("over");
}
16 }
输出:
    java.lang.ArithmeticException: / by zero
at test.exception.ExceptionDemo1.main(ExceptionDemo1.java:)
catch...
finally...
over

相信这段代码,问题不大


throw和return:都可以结束方法

 public class ExceptionDemo3 {

     public static void main(String[] args) {
try {
int b = ;
int res = /b;
System.out.println(res);
} catch (Exception e) {
System.out.println("catch...");
throw new RuntimeException(e.getMessage());
//System.out.println("catch...");
}finally{
System.out.println("finally...");
}
System.out.println("over");
} }

结果:

     catch...
finally...
Exception in thread "main" java.lang.RuntimeException: / by zero
at test.exception.ExceptionDemo3.main(ExceptionDemo3.java:)

如果catch有throw语句,在catch块中throw后面的语句执行不到(报错)。
此时仍然会执行finally语句,但不会执行finally后面的语句


 public class ExceptionDemo4 {

     public static void main(String[] args) {

         int i =test();
System.out.println(i);
} public static int test(){
int res=,b=; try {
res = /b;
} catch (Exception e) {
e.printStackTrace();
}finally{
res = ;
System.out.println("finally...");
}
return res; } }
     java.lang.ArithmeticException: / by zero
finally...
at test.exception.ExceptionDemo4.test(ExceptionDemo4.java:)
at test.exception.ExceptionDemo4.main(ExceptionDemo4.java:)

这里没什么好说的


 public class ExceptionDemo6 {
public static void main(String[] args) { int i =test();
System.out.println(i); } public static int test(){
int res=,b=; try {
res = /b;
} catch (Exception e) {
System.out.println("catch");
throw new RuntimeException(e.getMessage());
}finally{
res = ;
System.out.println("finally...");
}
return res;//不会执行
}
23 }

结果:

     catch
finally...
Exception in thread "main" java.lang.RuntimeException: / by zero
at test.exception.ExceptionDemo6.test(ExceptionDemo6.java:)
at test.exception.ExceptionDemo6.main(ExceptionDemo6.java:)

finally中的内容不论程序有无异常,都会被执行(除非在执行到finally之前jvm退出了),那么如果我们的程序在try和catch块中return了,finally中的还会执行吗?

故意把filename写错,造出异常,输出为下:

this is catch_for_filenot... block!
this is finally block!
this is main return value:false

从这儿看出来,程序先输出catch块中的,后又去执行finally块中的,虽然在catch中已经返回了,最后执行mian方法中的,而且输出false,说明catch块中的也成功返回了。

所以,面对疑问,我们可以很肯定的回答,即使有return语句,finally块也一定会被执行!



 public class FinallyDemo2 {

     public static void main(String[] args) {

        System.out.println(getInt());

     }

     public static int getInt() {

        int a = ;

        try {

            System.out.println(a / );

            a = ;

        } catch (ArithmeticException e) {

            a = ;

            return a;

     /*

     * return a在程序执行到这一步的时候,

       这里不是return a而是return 30;这个返回路径就形成了。

     * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40

        再次回到以前的返回路径,继续走return 30;

      */

        } finally {

            a = ;

        }

           return a;
}
}

可以尝试在finally中return a;

观察结果:返回40

因为新的返回路径(return 40)生成,覆盖原来的return 30

最新文章

  1. centos下在线安装mysql
  2. Hadoop日记Day18---MapReduce排序分组
  3. 修改LR自带的示例程序端口号
  4. [Js]布局转换
  5. P - The Shortest Path in Nya Graph-hdu4725(双端队列+拆点)
  6. Java 泛型数组
  7. mac下Android apk 破解流程
  8. iOS开发中怎么样使用激光推送
  9. python自动化运维学习第一天--day1
  10. Hbase问题
  11. Jmeter学习系列----3 配置元件之计数器
  12. docker 搭建maven 私服
  13. 使用HashSet<>去除重复元素的集合
  14. 你应该学会的接口调试神器——Postman高级用法
  15. 手动更新nexus的索引
  16. 关于Spring的一点东西
  17. tar 命令显示进度条
  18. HTML 入门1
  19. PIN 引脚类型
  20. 一个数据仓库时代开始--Hive

热门文章

  1. 2.自己的Github试用过程
  2. [Elixir006]CSV(Comma-separated values)处理
  3. osgi.net框架
  4. SQL Server—— 如何创建定时作业
  5. WebApi Post string 参数 为空
  6. 爬取lol皮肤
  7. “全栈2019”Java第一百零一章:局部内部类覆盖作用域内成员详解
  8. Java中Arrays工具类
  9. 使用java中for循环,循环打印出五角星--
  10. 如何到python模块路径linux