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. SpringBoot2.X 项目使用外置绝对路径的配置文件

    spring-boot-absolute-config 前言 该工程是为解决应用部署应用时指定配置文件存放位置的问题. SpringBoot项目默认加载以下位置的配置文件: ? 1 2 3 4 cla ...

  2. H3C 端口绑定典型配置举例

  3. linux /proc 接口

    无论何时一个硬件中断到达处理器, 一个内部的计数器递增, 提供了一个方法来检查设备 是否如希望地工作. 报告的中断显示在 /proc/interrupts. 下面的快照取自一个双处理 器 Pentiu ...

  4. ASP.NET MVC 实现页落网资源分享网站+充值管理+后台管理(3)之创建实体层

    实体层是介于表现层和业务层之间,同时也作为数据载体贯穿了整个项目之间的数据传递,创建实体有很多方法,我们可以手工创建,也可以代码生成引擎等等,我们这里主要应用数据实体模型连接生成: 创建好之后,我们需 ...

  5. CFEducational Codeforces Round 66题解报告

    CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...

  6. luoguP4313 文理分科

    luoguP4313 文理分科 复习完之后做了道典型题目. 这道题条件有点多 我们逐个分析 如果没有\(sameart\)或者\(samescience\)的限制,就是一个裸的最大权闭合子图的问题了 ...

  7. dotnet 删除只读文件

    如果直接通过 File.Delete 删除只读文件会出现没有权限提示,可以先设置文件不是只读然后删除文件 try { File.Delete(file); } catch (UnauthorizedA ...

  8. C#调用smtp邮件发送几个大坑

    1.网易.新浪邮箱新增了一个叫“授权码”的东西,开通smtp服务时,必须开启授权码,并且邮件发送代码中也需要加上授权码,如下代码: //指定邮箱账号和密码,需要注意的是,这个密码是你在邮箱设置里开启服 ...

  9. codeforces 1185G1 状压dp

    codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/118 ...

  10. Mybatis 框架

    在之前的内容中,我写了Java的基础知识.Java Web的相关知识.有这些内容就可以编写各种各样丰富的程序.但是如果纯粹手写所有代码,工作量仍然很大.为了简化开发,隐藏一些不必要的细节,专心处理业务 ...