码迷,mamicode.com
首页 > 其他好文 > 详细

异常处理

时间:2016-11-26 02:34:24      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:内容   style   valueof   cep   .sh   阅读   back   entry   and   

阅读以下代码(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, "输入不正确");

}

}

}

}

 结果:技术分享

技术分享

 
 

 

异常处理

标签:内容   style   valueof   cep   .sh   阅读   back   entry   and   

原文地址:http://www.cnblogs.com/xc166/p/6103251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!