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. H3C RIP基本配置举例

  2. java UDP传输

    ①:只要是网络传输,必须有socket . ②:数据一定要封装到数据包中,数据包中包括目的地址.端口.数据等信息. 直接操作udp不可能,对于java语言应该将udp封装成对象,易于我们的使用,这个对 ...

  3. ZR1050

    ZR1050 http://www.zhengruioi.com/problem/1030 题目大意: 给定一棵带点权的树,求所有联通块的点权和的平方的和 \(n \le 10^5\) 题解 首先,关 ...

  4. dotnet 通过 WMI 获取系统安装的驱动

    本文告诉大家如何通过 WMI 获取用户已经安装的驱动程序 通过 Win32_SystemDriver 可以获取用户已经安装的驱动程序 var mc = "Win32_SystemDriver ...

  5. wpf遮罩~~~(搬运过来的)

    方便自己以后用,原文:https://blog.csdn.net/lwwl12/article/details/78472235 直接上代码 public partial class BaseWind ...

  6. shelve模块、re模块

    在模糊匹配时使用 1

  7. 使用 Postman 测试你的 API

    使用 Postman 测试你的 API Intro 最近想对 API 做一些自动化测试,看了几个工具,最后选择了 postman,感觉 postman 的设计更好一些,我们可以在请求发送之前和请求获取 ...

  8. 0016 CSS 背景:background

    目标 理解 背景的作用 css背景图片和插入图片的区别 应用 通过css背景属性,给页面元素添加背景样式 能设置不同的背景图片位置 [插入图片,不用设置img元素的父元素.自身元素大小,即可见,但是背 ...

  9. 牛客国庆 Day4 H 巧妙的用树的直径!!

    传送门  https://ac.nowcoder.com/acm/contest/1109#question 刚开始吓得我以为要搞树分治,差点就捞了哦! 这个定理要铭记于心啊!!! #include& ...

  10. $Noip2013/Luogu1970$ 花匠 $dp$+思维

    $Luogu$ $Sol$ 和$Poj1037\ A\ Decorative\ Fence$好像吖. $f[i][0/1]$表示前$i$个数,且选了第$i$个数,这个数相对于上一个数是下降(上升)的, ...