zoukankan      html  css  js  c++  java
  • java理论学时第七节。课后作业。

    AboutException.java的理解。在try中如果发出某类系统识别的错误,会以throw的形式抛出,在catch中可以将其截获,不显示在前端,可以选择执行别的代码。

     

    ArrayIndexOutOfBoundsException/内层try-catch

    发生ArithmeticException

     

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

        }

      }

    运行结果。

    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

    总结。

    这些输出验证了trycatchfinally的运行顺序。与ifelse差不多。是先里层再外层的那种。

    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 finally不会被打印,可见catch中的退出语句生效了。但在try中退出就会报错,可见这么写是不符合规矩的。

    动手动脑。

    import javax.swing.JOptionPane;

    public class GradeRange

    {

      public static void main(String[] args)

      {

        try

        {

          String str;

          double grade;

          str = JOptionPane.showInputDialog("请输入某学生的成绩");

          //grade = Integer.parseInt(str);

          grade = Double.valueOf(str).doubleValue();

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

          {

            JOptionPane.showMessageDialog(null,"该学生不及格。");

          }

          else if(grade >= 60 && grade < 70)

          {

            JOptionPane.showMessageDialog(null,"该学生刚刚及格。");

          }

          else if(grade >= 70 && grade < 80)

          {

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

          }

          else if(grade >= 80 && grade < 90)

          {

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

          }

          else if(grade >= 90 && grade <= 100)

          {

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

          }

          else

          {

            JOptionPane.showMessageDialog(null,"输入数字不在成绩范围内。");

          }

        }

        catch(NumberFormatException e)

        {

          JOptionPane.showMessageDialog(null,"请输入数字。");

        }

      }

    }

     

     

     

  • 相关阅读:
    LeetCode 38. 外观数列
    LeetCode 33. 搜索旋转排序数组
    LeetCode 31. 下一个排列
    LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置
    LeetCode 29. 两数相除
    LeetCode 22. 括号生成
    LeetCode 1. 两数之和
    LeetCode 17. 电话号码的字母组合
    LeetCode 18. 四数之和
    LeetCode 16. 最接近的三数之和
  • 原文地址:https://www.cnblogs.com/shenshenxin/p/4961503.html
Copyright © 2011-2022 走看看