zoukankan      html  css  js  c++  java
  • java07课堂作业

    一、动手动脑:多层的异常捕获-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");

            }

        }

    }

    运行结果:

    ArrayIndexOutOfBoundsException/内层try-catch

    发生ArithmeticException

    二、动手动脑:多层的异常捕获-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");

            }

        }

    }

    运行结果:

    ArrayIndexOutOfBoundsException/外层try-catch

    三、请先阅读 EmbededFinally.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");

           

                      }

       

             }

    }

    运行结果:

    in Level 1

    in Level 2

    in Level 3

    Level 3:class java.lang.ArithmeticException

    In Level 3 finally

    In Level 2 finally

    In Level 1 finally

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

    四、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");

           

        }

       

    }

     

     

    }

     

    运行结果:

    in main

    Exception is thrown in main

     

    分析:在JVM正常运行的情况下,finally块一定会执行。但如果JVM都退出了finally块就无法执行了

    五、Java多层嵌套异常处理的基本流程

             http://lavasoft.blog.51cto.com/62575/18920/

    http://blog.sina.com.cn/s/blog_9d88a5770101gsf4.html

     

    六、成绩判断

    源代码:

    package 成绩判断;

    import javax.swing.JOptionPane;

    //<=60 “不及格”、60--69“及格”、70--79 “中”、80--89“良”、90-100“优”

    class NumberlimitedExceptiion extends Exception{

        NumberlimitedExceptiion(){

            JOptionPane.showMessageDialog(null, "所输入的数字超出范围!",

                    "输出",JOptionPane.INFORMATION_MESSAGE);

        }

    }

     

    class Shuru{

        int score;

        public static void prime(int m) throws NumberlimitedExceptiion{

            if(m>100||m<0){

                NumberlimitedExceptiion limitexcep=new NumberlimitedExceptiion();    

                throw limitexcep;   

                }

        }

        public void input(){

            try{

                String i=JOptionPane.showInputDialog("请输入一个整数成绩:");

                score=Integer.parseInt(i);

            }

            catch(NumberFormatException e){

                JOptionPane.showMessageDialog(null, "输入的不是整数!",

                       "输出",JOptionPane.INFORMATION_MESSAGE);

                input();

            }

            try{

                prime(score);

            } 

            catch(NumberlimitedExceptiion e) {      

                input();

            }

    //      finally{

    //         

    //      }

        }

    }

    public class Juage {

        public static void main(String[] args){

            Shuru a=new Shuru();

            a.input();

            if(a.score<60){

                JOptionPane.showMessageDialog(null, "此成绩为不及格!",

                       "输出",JOptionPane.INFORMATION_MESSAGE);

            }

            else if(a.score<=69){

                JOptionPane.showMessageDialog(null, "此成绩为及格!",

                       "输出",JOptionPane.INFORMATION_MESSAGE);

            }

            else if(a.score<=79){

                JOptionPane.showMessageDialog(null, "此成绩为中等!",

                       "输出",JOptionPane.INFORMATION_MESSAGE);

            }

            else if(a.score<=89){

                JOptionPane.showMessageDialog(null, "此成绩为良等!",

                       "输出",JOptionPane.INFORMATION_MESSAGE);

            }

            else if(a.score<=100){

                JOptionPane.showMessageDialog(null, "此成绩为优等!",

                       "输出",JOptionPane.INFORMATION_MESSAGE);

            }

     

        }

    }

     

  • 相关阅读:
    WebSocket
    Jedis工具类
    电脑突然没有声音了 右下角红叉叉,由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备。 (代码19)
    java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
    WCF中的数据契约(Continued)
    WCF中的服务契约
    搭建基于MOSS的团队解决方案01——Microsoft Office SharePoint 2007 Server快速入门(Continued)
    Silverlight 的发展之路
    Windows Workflow Foundation实验01——Windows Workflow Foundation快速入门(练习四)
    使用.NET平台工具操作Live Framework
  • 原文地址:https://www.cnblogs.com/420Rock/p/4960217.html
Copyright © 2011-2022 走看看