1. 如何捕獲異常
try
{
可能會出現異常的代碼段;
}
catch(異常類型名 處理該異常對象)
{
異常處理代碼段;
}
import java.io.*;
public class TryCatchTest {
public static void main(String[] args) {
File file = new File("abc.txt");
int a[] = {1, 2};
try
{
System.out.println(3/0);
}
catch(ArithmeticException e1)
{
System.out.println("3/0: ");
System.out.println("This is ArithmeticException");
}
try
{
System.out.println(a[2]);
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("a[2] is out of Array: ");
System.out.println("This is ArrayIndexOutOfBoundsException");
}
try
{
BufferedReader input = new BufferedReader(new FileReader(file));
}
catch (FileNotFoundException e3)
{
System.out.println("abc.txt is not found: ");
System.out.println("This is FileNotFoundException");
}
catch(IOException e)
{
System.out.println("This is IOException");
}
}
}
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語句拋出的異常或該異常的父類。
import java.io.*;
public class ThrowTest {
public void throwTest1() throws ArithmeticException
{
System.out.println(3/0);
}
public void throwTest2() throws ArrayIndexOutOfBoundsException
{
int a[] ={1,2};
System.out.println(a[2]);
}
public void throwTest3() throws FileNotFoundException
{
File file=new File("abc.txt");
new BufferedReader(new FileReader(file));
}
public void throwTest4() throws FileNotFoundException
{
throw new FileNotFoundException("abc.txt");
}
public static void main(String[] args) {
ThrowTest throwTest=new ThrowTest();
try
{
throwTest.throwTest1();
}
catch (ArithmeticException e1)
{
System.out.println("3/0: ");
System.out.println("This is ArithmeticException");
}
try
{
throwTest.throwTest2();
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("a[2] is out of Array: ");
System.out.println("This is ArrayIndexOutOfBoundsException");
}
try
{
throwTest.throwTest3();
}
catch (FileNotFoundException e3)
{
System.out.println("abc.txt is not found: ");
System.out.println("This is FileNotFoundException");
}
try
{
throwTest.throwTest4();
}
catch (FileNotFoundException e3)
{
System.out.println("abc.txt is not found: ");
System.out.println("This is FileNotFoundException");
}
}
}
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語句拋出。
public class MyException {
public static void main(String[] args) {
String str="2abcde";
try
{
char c=str.charAt(0);
if(c<'a'||c>'z'||c<'A'||c>'Z')
throw new FirstLetterException();
}
catch (FirstLetterException e)
{
System.out.println("This is FirstLetterException");
}
}
}
class FirstLetterException extends Exception{
public FirstLetterException()
{
super("The first char is not a letter");
}
public FirstLetterException(String str)
{
super(str);
}
}
This is FirstLetterException
public class MyException {
public static void main(String[] args) throws FirstLetterException{
throw new FirstLetterException();
}
}
class FirstLetterException extends Exception{
public FirstLetterException()
{
super("The first char is not a letter");
}
public FirstLetterException(String str)
{
super(str);
}
}
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語句才不會被執行而退出程序。
import java.io.*;
public class FinallyTest {
public static void main(String[] args) {
File file=null;
BufferedReader input=null;
file=new File("abc.txt");
try
{
input=new BufferedReader(new FileReader(file));
}
catch(FileNotFoundException e)
{
System.out.print("abc.txt is not found: ");
System.out.println("This is FileNotFoundException");
}
finally
{
System.out.println("This is finally code part.");
}
}
}
abc.txt is not found: This is FileNotFoundException
This is finally code part.