zoukankan      html  css  js  c++  java
  • 异常(Exception)

    1.Java中所有的异常类都会直接或间接地继承自Exception

    2.RuntimeException类也是直接继承自Exception类,它叫做运行时异常,Java中所有的运行时异常都会直接或间接地继承自RuntimeException

    3.Java中凡是继承自Exception而不是继承自RuntimeException的类都是非运行时异常。

    4.异常处理的一般结构是:

    try

    {

    }

    catch(Exception e)

    {

    }

    finally

    {

    }

    无论程序是否出现异常,finally块中的代码都是会被执行的。

     1 public class ExceptionTest
     2 {
     3     public static void main(String[] args) {
     4         int c = 0;
     5         try {
     6             int a = 3;
     7             int b = 0;
     8             // 出现异常后try中后面的代码不再运行
     9             c = a / b;// 每一个异常都是一个类,生成ArithmeticException的对象。
    10             System.out.println("hello world");
    11         }
    12         // 可以有多个catch
    13         catch (ArithmeticException e) {
    14             e.printStackTrace();// 打印栈的轨迹
    15         }
    16         // finally的代码一定会运行
    17         finally {
    18             System.out.println("welcome");
    19         }
    20         System.out.println(c);
    21     }
    22 }
    View Code

    5.对于非运行时异常(checked exception),必须要对其进行处理,处理方式有两种:第一种是使用try.. catchfinally进行捕获;第二种是在调用该会产生异常的方法所在的方法声明throws Exception

    throws Exception

     1 public class ExceptionTest2
     2 {
     3     //使用throws Exception抛出异常,当遇到异常时,程序暂停
     4     public void method() throws Exception//应为在方法中会抛出异常
     5     {
     6         System.out.println("hello world");
     7         throw new Exception();
     8     }
     9     public static void main(String[] args)
    10     {
    11         ExceptionTest2 test = new ExceptionTest2();
    12         try
    13         {
    14             test.method();
    15         }
    16         catch(Exception e)
    17         {
    18             e.printStackTrace();
    19         }
    20         finally
    21         {
    22             System.out.println("aaa");
    23         }
    24     }
    25 }
    View Code

    6.对于运行时异常(runtime exception),我们可以不对其进行处理,也可以对其进行处理。推荐不对其进行处理。

    7.NullPointerException是空指针异常,出现该异常的原因在于某个引用为null,但你却调用了它的某个方法。这时就会出现该异常。

    8.所谓自定义异常,通常就是定义了一个继承自Exception类的子类,那么这个类就是一个自定义异常类。通常情况下,我们都会直接继承自Exception类,一般不会继承某个运行时的异常类。

    9.我们可以使用多个catch块来捕获异常,这时需要将父类型的catch块放到子类型的catch块之后,这样才能保证后续的catch可能被执行,否则子类型的catch将永远无法到达,Java编译器会报编译错误;如果多个catch块的异常类型是独立的(MyException, MyException2), 那么谁前谁后都是可以的。

     1 public class MyException extends Exception
     2 {
     3     public MyException()
     4     {
     5         super();
     6     }
     7     
     8     public MyException(String message)
     9     {
    10         super(message);
    11     }
    12 }
    13 public class MyException2 extends Exception
    14 {
    15     public MyException2()
    16     {
    17         super();
    18     }
    19     
    20     public MyException2(String message)
    21     {
    22         super(message);
    23     }
    24 }
    25 
    26 public class ExceptionTest4
    27 {
    28     public void method(String str) throws Exception
    29     {
    30         if(null == str)
    31         {
    32             throw new MyException("传入的字符串参数不能为null");
    33         }
    34         else if("hello".equals(str))//把常亮放在前面,防止空指针异常
    35         {
    36             throw new MyException2("传入的字符串不能为hello");
    37         }
    38         else
    39         {
    40             System.out.println(str);
    41         }
    42     }
    43     public static void main(String[] args)
    44     {
    45         try
    46         {
    47             ExceptionTest4 test = new ExceptionTest4();
    48             
    49             test.method("hello");
    50         }
    51         //catch块按顺序匹配
    52         catch(MyException e)
    53         {
    54             System.out.println("进入到MyException catch块");
    55             e.printStackTrace();
    56         }
    57         catch(MyException2 e)
    58         {
    59             System.out.println("进入到MyException2 catch块");
    60             e.printStackTrace();
    61         }
    62         //这个catch不会运行
    63         catch(Exception e)
    64         {
    65             System.out.println("进入到Exception catch块");
    66             e.printStackTrace();
    67         }
    68         finally
    69         {
    70             System.out.println("异常处理完毕");
    71         }
    72         System.out.println("程序执行完毕");
    73     }
    74 }
    View Code

    10.如果try块中存在return语句,那么首先也需要将finally块中的代码执行完毕,然后方法再返回。

    11.如果try块中存在System.exit(0)语句,那么就不会执行finally块中的代码,因为System.exit(0)会终止当前运行的Java虚拟机,程序会在虚拟机终止前结束执行。

  • 相关阅读:
    CSUOJ 1081 集训队分组
    HDU 1865 More is Better
    HDU 1325 Is It A Tree?
    HDU 1272 小希的迷宫
    CSUOJ 1217 奇数个的那个数
    CSUOJ 1010 Water Drinking
    CSUOJ 1162 病毒
    CodeForces 295B
    CodeForces 20C
    SPOJ MULTQ3
  • 原文地址:https://www.cnblogs.com/daneres/p/4529379.html
Copyright © 2011-2022 走看看