try java
{程序員
可能會出現異常的代碼段;spa
}code
catch(異常類型名 處理該異常對象)對象
{blog
異常處理代碼段;繼承
}input
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 try 10 { 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 try 20 { 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 try 30 { 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 FileNotFoundExceptionit
編寫代碼過程當中,若是不想在這段代碼中捕捉和處理一個可能出現的異常,那麼就須要將這個異常傳遞出去,傳遞給調用它的方法去處理該異常。這個時候就須要使用throw 和throwsio
注意: 方法體中若使用了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 ArrayIndexOutOfBoundsException 11 { 12 int a[] ={1,2}; 13 System.out.println(a[2]); 14 } 15 16 public void throwTest3() throws FileNotFoundException 17 { 18 File file=new File("abc.txt"); 19 new BufferedReader(new FileReader(file)); 20 } 21 22 public void throwTest4() throws FileNotFoundException 23 { 24 throw new FileNotFoundException("abc.txt"); 25 } 26 27 public static void main(String[] args) { 28 ThrowTest throwTest=new ThrowTest(); 29 30 try 31 { 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 try 51 { 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 try 61 { 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
創建本身的異常類,要作的只是根據須要,從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)
在使用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 try 11 { 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 finally 20 { 21 System.out.println("This is finally code part."); 22 } 23 24 } 25 26 }
abc.txt is not found: This is FileNotFoundExceptionThis is finally code part.