• 动手动脑(异常处理)


    1.请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识

    import javax.swing.*;

     

    class AboutException {

       public static void main(String[] args) {

          float i=1, j=0, k;

          k=i/j;

          System.out.println(k);

        try

        {    

            k = i/j;    // Causes division-by-zero exception

            //throw new Exception("Hello.Exception!");

        }

        catch ( ArithmeticException e)

        {

            System.out.println("0.  "+ e.getMessage());

        }

        catch (Exception e)

        {

            if (e instanceof ArithmeticException)

                System.out.println("0");

            else

            {  

                System.out.println(e.getMessage());

                

            }

        }    

        finally

         {

                 JOptionPane.showConfirmDialog(null,"OK");

         }      

      }

    }

    结果:

    分析:

        Java 中所有可捕获的异常都派生自 Exception 类,把可能会发生错误的代码放进try语句块中。当程序检测到出现了一个错误时会抛出一个异常对象,异常处理代码会捕获并处理这个错误。catch语句块中的代码用于处理错误。当异常发生时,程序控制流程由try语句块跳转到catch语句块。

    try{

    //可能发生运行错误的代码;

    }

    catch(异常类型  异常对象引用){

    //用于处理异常的代码

    }

    finally{

    //用于“善后” 的代码

    }

     

    2.阅读以下代码(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");

            }

        }

    }

    结果:

             

    分析:

    当内层捕获异常并处理后,外层则不再捕获该异常。

     

    3.写出(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");

            }

        }

    }

    结果:

    分析:

    当异常未被处理时无法接受新的异常。

     

    4.请先阅读 EmbedFinally.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");               

                    }                               

                                }            

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

            }    

        }

    }

    结果:

    异常在level 3抛出

    异常在level 2抛出

    异常在level 1抛出

    分析:

    当外层异常未被处理时,内层异常不会被处理并且finally也不会执行,当有多层嵌套的finally语句时,异常在不同层次不同位置抛出时,也会导致不同的finally语句块执行顺序

     

    5.finally语句块一定会执行吗?请通过 SystemExitAndFinally.java示例程序回答上述问题

     

    结果:

    加上System.exit(0)

     

    分析:

    除非调用system.exit()让程序退出也就是将调用这个程序的进程断开了finally就不会执行,否则无论任何因素finally块都一定会执行。

     

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

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

    源程序:

    import java.util.Scanner;

    class GradeTest{

        private double grade;

        public void InputGrade(String s)

        {

            boolean flag = true;

            try

            {

                for(int i = 0;i<s.length();i++)

                {

                    if((s.charAt(i) < '0' || s.charAt(i) > '9')&& s.charAt(i) != '.')

                        flag = false;

                }

                if(!flag)

                {

                    throw new MyException("请输入数字!");

                }

                try{

                    grade = Double.parseDouble(s);

                    if(grade < 0 || grade > 100)

                    {

                        throw new MyException("请输入0100的数字!");

                    }

                    if(grade < 60)

                        System.out.println("不及格");

                    else if(grade <70)

                        System.out.println("及格");

                    else if(grade < 80)

                        System.out.println("");

                    else if(grade < 90)

                        System.out.println("");

                    else

                        System.out.println("");

                }

                catch(MyException e)

                {

                    System.out.println(e.getMessage());

                }

            }

            catch(MyException e)

            {

                System.out.println(e.getMessage());

            }

        }

    }

    class MyException extends Exception

    {

        MyException(String str)

        {

            super(str);

        }

    }

    public class Grade {

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            Scanner in = new Scanner(System.in);

            GradeTest G = new GradeTest();

            System.out.println("请输入成绩:");

            String str = in.nextLine();

            G.InputGrade(str);

            in.close();

        }

    }

    结果:

  • 相关阅读:
    SpringBlade 端口占用 Web server failed to start. Port 80 was already in use.
    SpringBlade 找不到或无法加载主类 springboot.Application
    Java idea 常用快捷键
    Java Velocity
    个人 一些需求
    Java MyBatis-Plus 基本使用
    Java Spring Initializr 创建的项目 包是一层一层的,需要隐藏一下空包
    MapReduce之自定义OutputFormat
    数据链路层之PPP协议
    MapReduce之GroupingComparator分组(辅助排序、二次排序)
  • 原文地址:https://www.cnblogs.com/sunqw/p/6103138.html
Copyright © 2020-2023  润新知