zoukankan      html  css  js  c++  java
  • 异常处理

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

    源代码:

    package demo;

    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

    写出CatchWho2.java程序运行的结果

    源代码:

    package demo;

    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

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

    题目: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");

    }

    }


    }

    截图:

    编写一个程序,此程序在运行时要求用户输入一个     整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

    要求程序必须具备足够的健壮性,不管用户输入什     么样的内容,都不会崩溃。

    源代码

    import javax.swing.JOptionPane;

    public class Test{

    public static void main(String[] args) {

    // TODO 自动生成的方法存根

    double n=0.0;

    for(int i=0;i<1000000000;i++)

    {

    String input=JOptionPane.showInputDialog("请输入学生成绩");

    try{

    n=Double.valueOf(input);

    if(n<0)

    {

    JOptionPane.showMessageDialog(null, "输入不正确");

    }

    else if(n<60)

    {

    JOptionPane.showMessageDialog(null,"该学生成绩为不及格");

    }

    else if(n<70)

    {

    JOptionPane.showMessageDialog(null,"该学生成绩为及格");

    }

    else if(n<80)

    {

    JOptionPane.showMessageDialog(null,"该学生成绩为中等");

    }

    else if(n<90)

    {

    JOptionPane.showMessageDialog(null,"该学生成绩为良好");

    }

    else if(n<=100)

    {

    JOptionPane.showMessageDialog(null,"该学生成绩为优秀");

    }

    else if(n>100)

    {

    JOptionPane.showMessageDialog(null, "输入不正确");

    }

    }

    catch(NumberFormatException e)//NumberFormatException异常

    {

    JOptionPane.showMessageDialog(null, "输入不正确");

    }

    }

    }

    }

     结果:

     
     
  • 相关阅读:
    h5 input调起摄像头、摄像机、录音机
    基数排序
    快速排序 && 希尔排序 && 插入排序
    堆排序
    归并排序(Merge sort)
    动态规划:背包问题
    Chap5: question: 29
    排列 && 组合
    Chap4: question: 19
    Chap3: question: 11
  • 原文地址:https://www.cnblogs.com/xc166/p/6103251.html
Copyright © 2011-2022 走看看