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.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 FileNotFoundException3. 自定义异常建立自己的异常类,要做的只是根据需要,从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 FirstLetterExceptionpublic 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.本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-07/119802.htm