• 07 异常处理


     

     

    Java中实现异常处理的基础知识。

    1)Java 中所有可捕获的异常都派生自 Exception 类。

    2)把可能会发生错误的代码放进try语句块中。

    3)当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。

    4)catch语句块中的代码用于处理错误。

    5)当异常发生时,程序控制流程由try语句块跳转到catch语句块。

    6)不管是否有异常发生,finally语句块中的语句始终保证被执行。

    7)如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

    Throwable类有两个直接子类:

    Exception:出现的问题是可以被捕获的;

    Error:系统错误,通常由JVM处理。

    可捕获的异常又可以分为两类:

    (1)Check异常:直接派生自Exception的异常类,必须被捕获或再次声明抛出

    (2)Runtime异常:派生自RuntimeException的异常类。使用throw语句可以随时抛出这种异常对象:

       throw new ArithmeticException(…);

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

                    

    }

                    

                   

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

    5、辨析:finally语句块一定会执行吗?

    1)只有与 finally 相对应的 try 语句块得到执行的情况下finally 语句块才会执行。以上两种情况�都是在 try 语句块之前返回return或者抛出异常�所以 try 对应的 finally 语句块没有执行。 

    2)当一个线程在执行 try 语句块或者 catch 语句块时被打段interrupted或者被终killed与其相对应的 finally 语句块可能不会执行。还有更极端的情况就是在线程运行 try 语句块或者catch语句块时突然死机或者断电finally语句块肯定不会执行了。可能有人认为死机、断电这些理由有些强词夺理没有关系我们只是为了说明这个问题。 

     

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

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

     

    程序代码:

    package demo;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class chengji {

    public static void main(String[] args) {

    BufferedReader b = new BufferedReader(new InputStreamReader(System.in));

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

    while(true) {

    try {

    String str = b.readLine();

    char ch;

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

    ch = str.charAt(i);

    if(!(ch >= '0' && ch <= '9'))

    throw new MyException("输入的必须是数字,请重新输入");

    }

    int score = Integer.parseInt(str);

    if(score > 100 || score < 0)

    throw new MyException("输入超过规定范围,请输入0-100之间的整数");

    if(score >= 90)

    System.out.println("该成绩为优");

    else if(score >= 80)

    System.out.println("该成绩为良");

    else if(score >= 70)

    System.out.println("该成绩为中");

    else if(score >= 60)

    System.out.println("该成绩为及格");

    else

    System.out.println("该成绩为不及格");

    break;

    }catch(MyException e){

    System.out.println(e);

    }catch(IOException e){

    System.out.println(e);

    }

    }

    }

    }

    class MyException extends Exception {

    public MyException(String s) {

    super(s);

    }

    }

    程序截图:

  • 相关阅读:
    VMWare磁盘配置的问题终于解决了!!
    十种老板不可追随
    关于ASP无组件上传在2003下出错
    设计模式的有趣解释-追MM[转]
    "未能在给定的程序集中找到任何适合于指定的区域性(或非特定区域性)的资源"解决办法
    今天看到了DNN3.0.4,感觉挺不错的,确实有很大的改进!!!
    今天加入了博客园
    一个女孩写的经典程序!!! (转载)
    加了强名后经常出现错误“程序集清单定义与程序集引用不匹配”
    C#写一个URL编码转换GB23121的方法,然后可以取到天气预报
  • 原文地址:https://www.cnblogs.com/xiaojq/p/7846895.html
Copyright © 2020-2023  润新知