1.

import javax.swing.*;

class AboutException {
public static void main(String[] a)
{
int i=1, j=0, k; try
{ k = i/j; // Causes division-by-zero exception
//throw new Exception("Hello.Exception!");
} // catch ( ArithmeticException e)
// {
// System.out.println("111被0除. "+ e.getMessage());
// } catch (Exception e)
{
if (e instanceof ArithmeticException)
System.out.println("被0除");
else
{
System.out.println(e.getMessage()); }
} finally
{
JOptionPane.showConfirmDialog(null,"OK");
}
}
}

2.

public class CatchWho {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
} throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}

运行结果:

ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException

3.

public class CatchWho2 {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e) {
System.out.println( "11ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("22发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.err.println( "33ArrayIndexOutOfBoundsException" + "/外层try-catch"); }
}
}

运行结果:

33ArrayIndexOutOfBoundsException/外层try-catch

4.

public class EmbededFinally {

    public static void main(String args[]) {

        int result;

        try {

            System.out.println("in Level 1");

             try {

                System.out.println("in Level 2");
// result=100/0; //Level 2 try { System.out.println("in Level 3"); result=100/0; //Level 3 } catch (Exception e) { System.out.println("Level 3:" + e.getClass().toString()); } finally { System.out.println("In Level 3 finally"); } // result=100/0; //Level 2 } catch (Exception e) { System.out.println("Level 2:" + e.getClass().toString()); }
finally { System.out.println("In Level 2 finally"); } // result = 100 / 0; //level 1 } catch (Exception e) { System.out.println("Level 1:" + e.getClass().toString()); } finally { System.out.println("In Level 1 finally"); } } }

结论:

当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

5.

public class SystemExitAndFinally {

    public static void main(String[] args)
{ try{ System.out.println("in main"); throw new Exception("Exception is thrown in main"); //System.exit(0); } catch(Exception e) { System.out.println(e.getMessage()); System.exit(0); } finally { System.out.println("in finally"); } } }

结论:

finally不一定会执行

20191102Java课堂记录的更多相关文章

  1. Java学习笔记二--API课堂记录

    JavaSE课堂记录(二) 第一节课 方法的重载:方法名相同,参数列表不同 方法的重写:方法名,参数列表相同 两同:方法名相同,参数列表相同 两小:访问权限小与等于父类,返回值类型小于等于父类(返回值 ...

  2. [C++讨论课] 课堂记录(一)

    今天第一次参加c++讨论课,记录下了各组同学的展示的问题或者解决方法,也有一些知识点上的内容,供以后复习参考. 1.常量指针和指针常量问题 常量指针:指向常量的指针,例如const int *p =  ...

  3. 20191014Java课堂记录

    1. Java字段初始化的规律 首先执行类成员定义时指定的默认值或类的初始化块,到底执行哪一个要看哪一个“排在前面”. 其次执行类的构造函数. 类的初始化块不接收任何的参数,而且只要一创建类的对象,它 ...

  4. 20190925Java课堂记录(二)

    1. testrandom public class test2{ public static void main(String[] args) { int[] n=new int [1001]; n ...

  5. 20190925Java课堂记录(一)

    判断字符串是否回文 设计思想 利用递归,每次返回长度减二的字符串,最终返回结果 源程序代码 import java.util.Scanner; public class palindrome { st ...

  6. 20190918Java课堂记录

    1. EnumTest.java public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; ...

  7. JavaWeb 学习009-4个页面,5条sql语句(添加、查看、修改、删除)

    ===========++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==+++++++++ 2016-12-3------ ...

  8. 机器学习笔记(三)Logistic回归模型

    Logistic回归模型 1. 模型简介: 线性回归往往并不能很好地解决分类问题,所以我们引出Logistic回归算法,算法的输出值或者说预测值一直介于0和1,虽然算法的名字有“回归”二字,但实际上L ...

  9. Cs231n课堂内容记录-Lecture 4-Part1 反向传播及神经网络

     反向传播 课程内容记录:https://zhuanlan.zhihu.com/p/21407711?refer=intelligentunit 雅克比矩阵(Jacobian matrix) 参见ht ...

随机推荐

  1. vue-axios当只调用vue.js又需要axios请求多时

    可以将axios方法封装一个函数 (function () { ASK = { get:function (url,data,succFun,errFun) { axios.get(url,{ par ...

  2. SpringDataJPA+QueryDSL玩转态动条件/投影查询

    在本文之前,本应当专门有一篇博客讲解SpringDataJPA使用自带的Specification+JpaSpecificationExecutor去说明如何玩条件查询,但是看到新奇.编码更简单易懂的 ...

  3. HTML常用布局

    一般的局部布局无非采用如下的技术: 1)div + ul(ol)-li:用于分类导航或菜单等场合     2)div + dl-dt-dd:用于图文混编场合     3)table-tr-td:用于图 ...

  4. springBoot中使用使用junit测试文件上传,以及文件下载接口编写

    本篇文章将介绍如何使junit在springBoot中测试文件的上传,首先先阅读如何在springBoot中进行接口测试. 文件上传操作测试代码 import org.junit.Before; im ...

  5. H3C 指定下次启动加载的应用程序文件

  6. 【p082】排座椅

    Time Limit: 1 second Memory Limit: 50 MB [问题描述] 上课的时候总有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了 ...

  7. vue-learning:4-template-v-if-and-v-show

    控制元素可见性的指令 v-if 和 v-show v-if v-else v-else-if :多重判断 template :分组渲染包裹元素 key:管理可复用元素 v-show v-if与v-sh ...

  8. 个人脚手架搭建 -- charmingsong-cli

    个人脚手架搭建 -- charmingsong-cli 目的 为了解决多次构建相同功能的项目,在一定程度上需要定制化以及私有化设置 设计 问题 为什么不用现成的脚手架生成? 如果利用vue或者reac ...

  9. nodejs的nvm与.net的dnvm使用对比

    一.vm安装命令 nodejs的nvm安装命令: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.s ...

  10. 对“TD信息树”的使用体验

    在本次同2017级学长进行的软件交流会上,我们有幸使用学长们开发的软件与成果,进过27个不尽相同的软件的使用,让我初步意识到了学习软件工程这门学科的实用价值.最终我选择了"TD信息树&quo ...