你的位置:首页 > Java教程

[Java教程]Java: 异常处理机制


1. 如何捕获异常

try 

{

可能会出现异常的代码段;

}

catch(异常类型名 处理该异常对象)

{

异常处理代码段;

}

 

 1 import java.io.*; 2  3 public class TryCatchTest { 4  5   public static void main(String[] args) { 6     File file = new File("abc.txt"); 7     int a[] = {1, 2}; 8      9     try10     {11       System.out.println(3/0);12     }13     catch(ArithmeticException e1)14     {15       System.out.println("3/0: ");16       System.out.println("This is ArithmeticException");17     }18     19     try20     {21       System.out.println(a[2]);22     }23     catch(ArrayIndexOutOfBoundsException e2)24     {25       System.out.println("a[2] is out of Array: ");26       System.out.println("This is ArrayIndexOutOfBoundsException");27     }28     29     try30     {31       BufferedReader input = new BufferedReader(new FileReader(file));32     }33     catch (FileNotFoundException e3)34     {35       System.out.println("abc.txt is not found: ");36       System.out.println("This is FileNotFoundException");37     }38     catch(IOException e)39     {40       System.out.println("This is IOException");41     }42 43   }44 45 }

3/0:
This is ArithmeticException
a[2] is out of Array:
This is ArrayIndexOutOfBoundsException
abc.txt is not found:
This is FileNotFoundException

 

2. 如何抛出异常

编写代码过程中,如果不想在这段代码中捕捉和处理一个可能出现的异常,那么就需要将这个异常传递出去,传递给调用它的方法去处理该异常。这个时候就需要使用throw 和throws

  • throws语句在方法声明中使用,抛出异常
  • throw语句在方法体内部使用,抛出异常

注意: 方法体中若使用了throw语句抛出异常,则必须在该方法声明中,采用throws语句来声明该方法体中抛出的异常,同时,throws语句声明抛出的异常,必须是方法体中throw语句抛出的异常或该异常的父类。

 1 import java.io.*; 2  3 public class ThrowTest { 4    5   public void throwTest1() throws ArithmeticException 6   { 7     System.out.println(3/0); 8   } 9   10   public void throwTest2() throws ArrayIndexOutOfBoundsException11   {12     int a[] ={1,2};13     System.out.println(a[2]);14   }15   16   public void throwTest3() throws FileNotFoundException17   {18     File file=new File("abc.txt");19     new BufferedReader(new FileReader(file));20   }21   22   public void throwTest4() throws FileNotFoundException23   {24     throw new FileNotFoundException("abc.txt");25   }26 27   public static void main(String[] args) {28     ThrowTest throwTest=new ThrowTest();29     30     try31     {32       throwTest.throwTest1();33     }34     catch (ArithmeticException e1)35     {36       System.out.println("3/0: ");37       System.out.println("This is ArithmeticException");38     }39     40     try 41     {42       throwTest.throwTest2();43     }44     catch(ArrayIndexOutOfBoundsException e2)45     {46       System.out.println("a[2] is out of Array: ");47       System.out.println("This is ArrayIndexOutOfBoundsException");48     }49     50     try51     {52       throwTest.throwTest3();53     }54     catch (FileNotFoundException e3)55     {56       System.out.println("abc.txt is not found: ");57       System.out.println("This is FileNotFoundException");58     }59     60     try61     {62       throwTest.throwTest4();63     }64     catch (FileNotFoundException e3)65     {66       System.out.println("abc.txt is not found: ");67       System.out.println("This is FileNotFoundException");68     }69 70   }71 72 }

3/0:
This is ArithmeticException
a[2] is out of Array:
This is ArrayIndexOutOfBoundsException
abc.txt is not found:
This is FileNotFoundException
abc.txt is not found:
This is FileNotFoundException

 

3. 自定义异常

建立自己的异常类,要做的只是根据需要,从Exception类或者从Exception类的子类中继承出需要的类。习惯上,会经常为每一个异常类,提供一个默认的和一个包含详细信息的构造器。需要注意的是,自定义异常类,必须由程序员使用throw语句抛出。

 1 public class MyException { 2  3   public static void main(String[] args) { 4     String str="2abcde"; 5      6     try 7     { 8       char c=str.charAt(0); 9       if(c<'a'||c>'z'||c<'A'||c>'Z')10         throw new FirstLetterException();11     }12     catch (FirstLetterException e)13     {14       System.out.println("This is FirstLetterException");15     }16 17   }18 19 }20 21 class FirstLetterException extends Exception{22   public FirstLetterException()23   {24     super("The first char is not a letter");25   }26   27   public FirstLetterException(String str)28   {29     super(str);30   }31 }

This is FirstLetterException

 

 1 public class MyException { 2  3   public static void main(String[] args) throws FirstLetterException{ 4     throw new FirstLetterException(); 5   } 6 } 7  8 class FirstLetterException extends Exception{ 9   public FirstLetterException()10   {11     super("The first char is not a letter");12   }13   14   public FirstLetterException(String str)15   {16     super(str);17   }18 }

Exception in thread "main" FirstLetterException: The first char is not a letter
at MyException.main(MyException.java:5)

 

4. 使用finally语句

在使用try...catch语句是,若try语句中的某一句出现异常情况,那么这部分try语句段中,从出现异常的语句开始,之后的所有语句都不会被执行,直到这部分try语句段结束。

但是在很多情况下,希望无论是否出现异常,某些语句都需要被执行。那么就可以把这部分代码放在finally语句段中,即使try或catch语句段中含有return语句,程序都会在异常抛出后先执行finally语句段,除非try或catch语句段中执行System.exit()方法,或者是出现Error错误,finally语句才不会被执行而退出程序。

 1 import java.io.*; 2  3 public class FinallyTest { 4  5   public static void main(String[] args) { 6     File file=null; 7     BufferedReader input=null; 8     file=new File("abc.txt"); 9     10     try11     {12       input=new BufferedReader(new FileReader(file));13     }14     catch(FileNotFoundException e)15     {16       System.out.print("abc.txt is not found: ");17       System.out.println("This is FileNotFoundException");18     }19     finally20     {21       System.out.println("This is finally code part.");22     }23 24   }25 26 }

abc.txt is not found: This is FileNotFoundException
This is finally code part.