31、异常(Exception)

Java程序运行时会出现错误(Error)和异常(Exception),错误不能在进行处理,是致命的,异常还可以进行处理。

java中的异常分为两大类:

   A)checked exception(非Runtime Exception)

   B)Unchecked exception(Runtime Exception)

Java中所有的异常类都会直接或间接继承自Exception;RuntimeException类直接继承自Exception,他叫做运行时异常,java中所有运行时异常都直接或间接继承自RuntimeException。java中凡是继承自Exception而不是继承自RuntimeException的类都是非运行时异常。

1、

异常处理的一般结构:

   Try

{

}

Catch(Exception e

{

}

Finally

{

}

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

执行流程:先执行try块中代码,出现异常,终止执行,转到catch,执行完catch后执行finally,如果没有异常,执行完try,执行finally。

2、在方法中可以使用throw new Exception();手工抛出异常。如果在方法中抛出了异常,在方法声明中也要声明抛出异常,在方法声明后加上throws Exception。

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

[java] view plain copy print ?
  1. public class ExceptionTest2 
  2.     public void method() throws Exception 
  3.     { 
  4.         System.out.println("hello"); 
  5.          
  6.         throw new Exception(); 
  7.     } 
  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("world"); 
  23.         } 
  24.     } 

对于运行时异常(RuntimeException),我们可以不对其进行处理,也可以对其进行处理,建议不处理

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

[java] view plain copy print ?
  1. public class ExceptionTest3 
  2.     public static void main(String[] args) 
  3.     { 
  4.         String str = null
  5.          
  6.         System.out.println(str.length());//出现NullPointrException异常 
  7.     } 


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

自定义异常及两种处理方式:

[java] view plain copy print ?
  1. public class ExceptionTest4 
  2.     public void method(String str) throws MyException 
  3.     { 
  4.         if (null == str) 
  5.         { 
  6.             throw new MyException("传入字符串不能为null"); 
  7.         } 
  8.         else 
  9.         { 
  10.             System.out.println(str); 
  11.         } 
  12.     } 
  13.      
  14. //  public static void main(String[] args) throws MyException 
  15. //  { 
  16. //      ExceptionTest4 test = new ExceptionTest4(); 
  17. //       
  18. //      test.method(null); 
  19. //  } 
  20.      
  21.     public static void main(String[] args) 
  22.     { 
  23.         ExceptionTest4 test = new ExceptionTest4(); 
  24.          
  25.         try 
  26.         { 
  27.             test.method(null); 
  28.         } 
  29.         catch(MyException e) 
  30.         { 
  31.             e.printStackTrace(); 
  32.         } 
  33.         finally 
  34.         { 
  35.             System.out.println("异常处理完毕"); 
  36.         } 
  37.         System.out.println("程序处理完毕"); 
  38.     } 


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

[java] view plain copy print ?
  1. public class ExceptionTest5 
  2.     public void method(String str) throws MyException,MyException2 
  3.     { 
  4.         if (null == str) 
  5.         { 
  6.             throw new MyException("传入字符串不能为null"); 
  7.         } 
  8.         else if("hello" == str) 
  9.         { 
  10.             throw new MyException2("传入了hello"); 
  11.         } 
  12.         else 
  13.         { 
  14.             System.out.println(str); 
  15.         } 
  16.     } 
  17.      
  18. //  public static void main(String[] args) throws MyException 
  19. //  { 
  20. //      ExceptionTest4 test = new ExceptionTest4(); 
  21. //       
  22. //      test.method(null); 
  23. //  } 
  24.      
  25.     public static void main(String[] args) 
  26.     { 
  27.          
  28.          
  29.         try 
  30.         { 
  31.             ExceptionTest5 test = new ExceptionTest5(); 
  32.             test.method("hello"); 
  33.         } 
  34.         catch(MyException e) 
  35.         { 
  36.             e.printStackTrace(); 
  37.         } 
  38.         catch(MyException2 e) 
  39.         { 
  40.             e.printStackTrace(); 
  41.         } 
  42.         catch(Exception e) //必须放在最后,否则它将拦截所有异常,后面的catch将再无法获得运行时机 
  43.         { 
  44.             e.printStackTrace(); 
  45.         } 
  46.         finally 
  47.         { 
  48.             System.out.println("异常处理完毕"); 
  49.         } 
  50.         System.out.println("程序处理完毕"); 
  51.     } 


6、如果try块中存在return语句,那么首先也需要将finally块中的代码执行完毕,然后方法再返回,再执行return语句;如果try块中存在System。Exit(0)语句,那么就不会执行finally块中的代码,因为System。Exit(0)会终止当前运行的java虚拟机,程序会在虚拟机终止前结束执行。

你可能感兴趣的:(java开发工具)