1>请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。

import javax.swing.*;

class AboutException {

public static void main(String[] a)

{

float i=1, j=0, k;

k=i/j;

System.out.println(k);

try

{

k = i/j;    // Causes division-by-zero exception

//throw new Exception("Hello.Exception!");    }

catch ( ArithmeticException e)

{

System.out.println("被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");

}

}

}

异常处理:Java中异常捕获语句

try{用于监控可能发生错误的语句}

catch(异常类型 异常对象引用)

{ 用于捕获并处理异常的代码 }

finally

{ //用于“善后” 的代码 }

不管是否有异常发生,finally语句块中的语句始终保证被执行。

2>阅读以下代码(CatchWho.java),写出程序运行结果:

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>写出CatchWho2.java程序运行的结果

public class CatchWho2 {

public static void main(String[] args) {

try {

try {

throw new ArrayIndexOutOfBoundsException();

}

catch(ArithmeticException 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

结果分析:当异常未被处理时无法接受新的异常。

4>请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。

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语句时,异常在不同层次不同位置抛出时,也会导致不同的finally语句块执行顺序。

5>finally语句块一定会执行吗?

请通过 SystemExitAndFinally.java示例程序回答上述问题

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对应的try语句得到执行的情况下finally语句才会执行,但如果finally语句之前出现例如System.exit(0) 等使Java虚拟机停止运行的语句时finally语句也不会被执行。

备注:

  • 所有派生于Throwable类的异常类,基本都没有这些成员方法,也就是说所有的异常类都只是一个标记,记录发生了什么类型的异常(通过标记,编译期和JVM做不同的处理),所有实质性的行为Throwable都具备了。

  • 综上,在一个Throwable里面可以获取什么信息?

    • 获取堆栈跟踪信息(源代码中哪个类,哪个方法,第几行出现了问题……从当前代码到最底层的代码调用链都可以查出来)

    • 获取引发当前Throwable的Throwable。追踪获取底层的异常信息。

    • 获取被压抑了,没抛出来的其他Throwable。一次只能抛出一个异常,如果发生了多个异常,其他异常就不会被抛出,这时可以通过加入suppressed异常列表来解决(JDK7以后才有)。

    • 获取基本的详细描述信息

动手动脑java异常处理的更多相关文章

  1. 动手动脑-java重载

    有以下例子: 例: Using overloaded methods public class MethodOverload { public static void main(String[] ar ...

  2. 动手动脑——JAVA语法基础

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

  3. 动手动脑-Java的方法重载

    例: Using overloaded methods public class MethodOverload {  public static void main(String[] args) {  ...

  4. 动手动脑-Java的继承与多态

    一. class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); ...

  5. java异常处理动手动脑问题解决和课后总结

    动手动脑 一.问题:请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. 1.源代码 import javax.swing.*; cl ...

  6. 课堂动手动脑验证以及自定义异常类实现对异常处理——java异常类

    异常(exception):发生在程序执行期间,表明出现了一个非法运行的情况.许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象.例如:数组越界和被0除. 代码验证: package test ...

  7. JAVA语法基础作业——动手动脑以及课后实验性问题 (八)

    一.动手动脑 运行AboutException.java示例,了解Java中实现异常处理的基础知识. 1)源代码 import javax.swing.*; class AboutException ...

  8. JAVA09异常处理之动手动脑问题

    动手动脑1:为什么不管是否有异常发生,finally语句块中的语句始终保证被执行? 我们在写代码时,如果finally块中的代码过多会导致字节码条数"膨胀",因为finally中的 ...

  9. JAVA方法03之动手动脑问题解决

    动手动脑1.当JAVA里定义的函数中去掉static后,怎么办?(如下程序,将square()函数的static去掉) public class SquareIntTest { public stat ...

随机推荐

  1. topcoder srm 702 div1 -3

    1.给定一个$n*m$的矩阵,里面的数字是1到$n*m$的一个排列.一个$n*m$矩阵$A$对应一个$n*m$ 的01字符串,字符串的位置$i*m+j$为1当且仅当$A_{i,j}=i*m+j+1$. ...

  2. tp剩余未验证内容

    new Image(宽度,高度) $(image).attr('src', ...).load(function(){....}) load表示浏览器从服务器下载(装载)对象完成, 这个load方法很 ...

  3. SpringBoot 使用Sharding-JDBC进行分库分表及其分布式ID的生成

    为解决关系型数据库面对海量数据由于数据量过大而导致的性能问题时,将数据进行分片是行之有效的解决方案,而将集中于单一节点的数据拆分并分别存储到多个数据库或表,称为分库分表. 分库可以有效分散高并发量,分 ...

  4. 【20K必备知识点】北上广Java开发月薪20K往上,该如何做,需要会写什么

    有人回答说这只能是大企业或者互联网企业工程师才能拿到.也许是的,小公司或者非互联网企业拿两万的不太可能是码农了,应该已经转管理.还有区域问题,这个不在我的考虑范围内,因为除了北上广深杭,其他地方也很难 ...

  5. C# winform combobox默认选中项方法

    https://blog.csdn.net/easyboot/article/details/68062196 可以使用 Combobox.SelectText = “默认选中文本”; 但是如果Com ...

  6. 用yarn代替cnpm,cnpm漏包有点严重

    npm 的方式  npm  install  -g  yarn   安装完成后,你可以测试下自己的版本 yarn --version 开始使用 单独安装包的方式add 不是install,后面不用加 ...

  7. [idea] - 项目启动报错Process finished with exit code 1

    今天运行项目发现一个bug, "C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" -XX:TieredStopAtLevel=1 - ...

  8. HDU 3635 Dragon Balls(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...

  9. eclipse中建geoserver源码

    概述:本文讲述的是在eclipse中如何构建geoserver源码工程,其中涉及到了jdk,github,marven等. 1.安装git 从(http://git-scm.com/download/ ...

  10. 百度地图自定义icon,定位偏移问题

    最近使用百度地图做一个调度系统,使用定义icon的marker,结果地图显示marker和实际位置偏移,最终参考文章: http://www.cnblogs.com/jz1108/archive/20 ...