zoukankan      html  css  js  c++  java
  • 动手动脑7

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

            } 

        } 

    }

    运行结果:

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

            } 

        } 

    }

    运行结果:

    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语句块执行顺序。

    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块中的代码一定会执行

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

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

    方法1

    import javax.swing.JOptionPane;

    public class Mark {

    public static void main(String[] args) {

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

              

               double i=0.0;

               for(int w=0;w<100000000;w++)

               {

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

               try{

               i=Double.valueOf(input);

                

                if(i>=0&&i<60)

                    {

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

                    break;

                    }

                    if(i>=60&&i<70)

                    {

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

                    break;

                    }

                    if(i>=70&&i<80)

                    {

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

                    break;

                    }

                    if(i>=80&&i<90)

                    {

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

                    break;

                    }

                    if(i>=90&&i<=100)

                    {

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

                    break;

                    }  

                if(i<0||i>100)

                {

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

                }

              

               }

              catch(NumberFormatException ex){

                JOptionPane.showConfirmDialog(null, "输入不正确,请重新输入");

              }

               }  

    }

    }

    运行结果:

    方法2

    import javax.swing.JOptionPane;

    public class Mark {

    public static void main(String[] args) {

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

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

                {

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

                  int l;double i=0.0;;

                  l=input.length(); 

                 char c[]=input.toCharArray();

                  for(int x=0;x<l;x++)

                  {

                  if(c[x]>57||c[x]<47)

                  {

                 i=101;

                  }

                  else 

                  {

                   i=Double.valueOf(input); 

                  }

                  }

                

                 if(i>100||i<0)

                 {

                 JOptionPane.showConfirmDialog(null, "请重新输入学生成绩");

                 }

                 if(i>=0&&i<60)

                 {

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

                 break;

                 }

                 if(i>=60&&i<70)

                 {

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

                 break;

                 }

                 if(i>=70&&i<80)

                 {

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

                 break;

                 }

                 if(i>=80&&i<90)

                 {

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

                 break;

                 }

                 if(i>=90&&i<=100)

                 {

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

                 break;

                 }

                }

    }

    }

    运行结果:

  • 相关阅读:
    关于hibernate的缓存使用(转)
    Webservice 实践
    三大电商注册登录表单分析
    网上好文搜集
    git简介
    Python程序中的进程操作--—--开启多进程
    进程的创建和结束
    同步异步阻塞和非阻塞
    进程的并行和并发
    进程调度
  • 原文地址:https://www.cnblogs.com/muamu/p/4960298.html
Copyright © 2011-2022 走看看