zoukankan      html  css  js  c++  java
  • 异常处理----使用 try…catch…finally 处理异常

    使用 try…catch…finally 处理异常


    异常处理是通过try-catch-finally语句实现的。
      try {
        ...... //可能产生异常的代码
      } catch( ExceptionName1 e ) {
        ...... //当产生ExceptionName1型异常时的处置措施
      } catch( ExceptionName2 e ) {
        ...... //当产生ExceptionName2型异常时的处置措施
      } [finally {
        ...... //无条件执行的语句
      } ]

    异常处理举例
    public class Test8_2 {
      public static void main(String[] args) {
        String friends[]={"lisa","bily","kessy"};
        try {
          for(int i=0;i<5;i++) {
            System.out.println(friends[i]);
          }
        } catch(ArrayIndexOutOfBoundsException e) {
          System.out.println("index err");
        }
        System.out.println(" this is the end");
      }
    }

    运行结果:
      lisa
      bily
      kessy
      index err

      this is the end

    public class DivideZero1{
      int x;
      public static void main(String[] args) {
        int y;
        DivideZero1 c=new DivideZero1();
        try{
          y=3/c.x;
        } catch(ArithmeticException e){
          System.out.println("divide by zero error!");
        }
        System.out.println("program ends ok!");
      }
    }

    运行结果:
      divide by zero error!
      program ends ok!


     1 public class TestTryCatchFinally {
     2     public static void main(String[] args) {
     3         
     4         try {
     5             int i = 10;
     6             int j = i / 0;
     7         }catch (NullPointerException e) {
     8             System.out.println(e.getMessage());
     9         }catch(ClassCastException e) {
    10             System.out.println(e.getMessage());
    11         }catch(Exception e) {
    12             System.out.println(e.getMessage());
    13         } finally{
    14              System.out.println("finally...");
    15          }
    16         
    17         //不论在try、catch代码块中是否发生了异常事件,finally块中的语句都会被执行。
    18         
    19         System.out.println("end...");
    20         
    21         //示例编译时异常, IO 异常属于编译时异常. 
    22         
    23         try {
    24             InputStream is = new FileInputStream("abc.txt");
    25         } catch (FileNotFoundException e) {
    26             e.printStackTrace();
    27         }
    28     }
    29 }

    捕获异常


    try
    捕获异常的第一步是用try{…}语句块选定捕获异常的范围,将可能出现异常的代码放在try语句块中。
    catch (Exceptiontype e)
    在catch语句块中是对异常对象进行处理的代码。每个try语句块可以伴随一个或多个catch语句,用于处理可能产生的不同类型的异常对象。

    如果明确知道产生的是何种异常,可以用该异常类作为catch的参数;也可以用其父类作为catch的参数
    可以用ArithmeticException类作为参数,也可以用RuntimeException类作为参数,或者用所有异常的父类Exception类作为参数。但不能是与ArithmeticException类无关的异常,如NullPointerException,那么,catch中的语句将不会执行。

    捕获异常的有关信息:
    与其它对象一样,可以访问一个异常对象的成员变量或调用它的方法。
    getMessage( ) 方法,用来得到有关异常事件的信息
    printStackTrace( )用来跟踪异常事件发生时执行堆栈的内容。

    finally
    捕获异常的最后一步是通过finally语句为异常处理提供一个统一的出口,使得在控制流转到程序的其它部分以前,能够对程序的状态作统一的管理。不论在try、catch代码块中是否发生了异常事件,finally块中的语句都会被执行
    finally语句是可选的

    运行时异常和编译时异常


    前面但使用的异常都是RuntimeException类或是它的子类,这些类的异常的特点是:即使没有使用try和catch捕获,Java自己也能捕获,并且编译通过 ( 但运行时会发生异常使得程序运行终止 )

    如果抛出的异常是IOException类的异常,则必须捕获,否则编译错误


    IOException 异常处理举例

    public class Test8_3{
      public static void main(String[] args) {
        FileInputStream in=new FileInputStream("myfile.txt");
        int b;
        b = in.read();
        while(b!= -1) {
          System.out.print((char)b);
         b = in.read();
        }
        in.close();
      }
    }


    public class Test8_3 {
      public static void main(String[] args) {
        try{
          FileInputStream in=new FileInputStream("myfile.txt");
          int b;
          b = in.read();
          while(b!= -1) {
            System.out.print((char)b);
            b = in.read();
          }
          in.close();
        }catch (IOException e) {
          System.out.println(e);
        }finally {
          System.out.println(" It’s ok!");
        }
      }
    }

  • 相关阅读:
    shell脚本100例、练习使用
    shell基础编程
    mysql基础理论知识
    Docker 基础
    python基础之类(面向对象编程)
    python基础之函数
    python基础之认知及编码
    python基础之数据类型
    python基础之注意事项
    1.linux使用基础
  • 原文地址:https://www.cnblogs.com/justdoitba/p/7233246.html
Copyright © 2011-2022 走看看