try
{
可能會(huì)出現(xiàn)異常的代碼段;
}
catch(異常類型名 處理該異常對(duì)象)
{
異常處理代碼段;
}
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 ArithmeticExceptiona[2] is out of Array: This is ArrayIndexOutOfBoundsExceptionabc.txt is not found: This is FileNotFoundException
2. 如何拋出異常編寫代碼過(guò)程中,如果不想在這段代碼中捕捉和處理一個(gè)可能出現(xiàn)的異常,那么就需要將這個(gè)異常傳遞出去,傳遞給調(diào)用它的方法去處理該異常。這個(gè)時(shí)候就需要使用throw 和throws
注意: 方法體中若使用了throw語(yǔ)句拋出異常,則必須在該方法聲明中,采用throws語(yǔ)句來(lái)聲明該方法體中拋出的異常,同時(shí),throws語(yǔ)句聲明拋出的異常,必須是方法體中throw語(yǔ)句拋出的異常或該異常的父類。
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 ArithmeticExceptiona[2] is out of Array: This is ArrayIndexOutOfBoundsExceptionabc.txt is not found: This is FileNotFoundExceptionabc.txt is not found: This is FileNotFoundException
3. 自定義異常建立自己的異常類,要做的只是根據(jù)需要,從Exception類或者從Exception類的子類中繼承出需要的類。習(xí)慣上,會(huì)經(jīng)常為每一個(gè)異常類,提供一個(gè)默認(rèn)的和一個(gè)包含詳細(xì)信息的構(gòu)造器。需要注意的是,自定義異常類,必須由程序員使用throw語(yǔ)句拋出。
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 letterat MyException.main(MyException.java:5)
4. 使用finally語(yǔ)句在使用try...catch語(yǔ)句是,若try語(yǔ)句中的某一句出現(xiàn)異常情況,那么這部分try語(yǔ)句段中,從出現(xiàn)異常的語(yǔ)句開始,之后的所有語(yǔ)句都不會(huì)被執(zhí)行,直到這部分try語(yǔ)句段結(jié)束。
但是在很多情況下,希望無(wú)論是否出現(xiàn)異常,某些語(yǔ)句都需要被執(zhí)行。那么就可以把這部分代碼放在finally語(yǔ)句段中,即使try或catch語(yǔ)句段中含有return語(yǔ)句,程序都會(huì)在異常拋出后先執(zhí)行finally語(yǔ)句段,除非try或catch語(yǔ)句段中執(zhí)行System.exit()方法,或者是出現(xiàn)Error錯(cuò)誤,finally語(yǔ)句才不會(huì)被執(zhí)行而退出程序。
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 FileNotFoundExceptionThis is finally code part.
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注