博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
动手动脑之异常处理
阅读量:4620 次
发布时间:2019-06-09

本文共 5430 字,大约阅读时间需要 18 分钟。

1.阅读代码(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");         }     } }

2.阅读代码(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");         }     } }

3.阅读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");                }        }}

原因:当有多个嵌套的try…catch…finally时,异常在不同位置被接受,可能会导致异常下面的finally语句块执行顺序。不管是否有异常发生,finally语句块中的语句始终保证被执行。

4.finally语句一定会被执行吗

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");               }     }}

不会,System.exit(0)可以终止程序,finally语句块一定会执行。

 5.自行归纳Java多层嵌套异常处理的基本流程

在java语言中,通常将可能出现异常的语句放入try{}语句中,将出现错误后需要执行的语句放入到catch{}语句中,将无论是否发生异常都要执行的语句放在finally{}语句中。当程序执行出现异常的时候,系统会抛出一个异常,然后由try{}语句中中出现异常的地方转到catch{}语句中。不过不管有没有异常产生,finally{}中的语句都将执行。如果系统出现系统错误或者运行Runtime异常,jvm会结束程序运行,不一定会执行finally{}中的语句。如果try{}中产生的异常在catch中没有处理,系统将停止程序,也不会执行finally中的语句。

6.动手动脑,编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

import java.util.Scanner;public class Test {    public static void main(String[] args)    {        Scanner input=new Scanner(System.in);        System.out.println("请输入分数:");        String score=input.nextLine();        while(true)        {        try        {            for(int i=0;i
=48&&score.charAt(i)<=57)) { throw new MyException(); } } int m = Integer.parseInt(score);//把字符串转换成整型 String str=""; if(m<60) { str="不及格"; } else if(m<70) { str="及格"; } else if(m<80) { str="中"; } else if(m<90) { str="良"; } else if(m<=100) { str="优"; } else if (m>100||m<0) { throw new MyException(); } System.out.println("分数等级为:"+str); break; } catch(MyException A) { A.error(); } } }}class MyException extends Exception{ public void error() { System.out.println("你输入的数字不符合要求"); }}

 

转载于:https://www.cnblogs.com/lijing925/p/7846873.html

你可能感兴趣的文章
Java 变参函数的实现
查看>>
nrf51 SDK自带例程的解读
查看>>
SESSION技术
查看>>
数据结构(五)之直接插入排序
查看>>
SQL函数——LENGTH()和LENGTHB()
查看>>
vim - manual -个人笔记
查看>>
详解Javascript中prototype属性(推荐)
查看>>
angularjs实现首页轮播图
查看>>
Git 对象 和checkout 和stash的笔记
查看>>
团队项目总结2-服务器通信模型和顺序图
查看>>
hdu 1085 Holding Bin-Laden Captive!
查看>>
[周记]8.7~8.16
查看>>
递归定义
查看>>
kindeditor 代码高亮设置
查看>>
图的邻接表存储
查看>>
2018 leetcode
查看>>
各浏览器对 onbeforeunload 事件的支持与触发条件实现有差异
查看>>
PHP中获取当前页面的完整URL
查看>>
所谓输入掩码技术,即只有数字键起作用
查看>>
Display对象,Displayable对象
查看>>