JAVA學習筆記26——異常機制2:常見異常+異常處理其餘兩種方法+自定義異常

最近在看JAVA教學的視頻,覺得老師講的很好,同時借用源代碼還有筆記來撰寫本系列博客,記錄自己的學習內容,同時也供看到的人學習。

本篇將異常剩餘的基礎內容介紹完畢。

上一篇筆記寫的時候漏掉了一個內容:常見的幾種異常的介紹,這裏補上。常見異常分類:checked Exception、unchecked Exception兩大類。


最常見的異常類是“空指針異常(NullPointerException)”。以下是幾種常見異常的處理辦法:



接下來介紹異常處理方法之二:聲明異常(throws語句)和異常處理方法之三:手動拋出異常(throw子句)



下面的兩個代碼片是兩種方法的實例代碼:

import java.io.File;
import java.io.FileNotFoundException;
public class TestException {
	public static void main(String[] args) {
//		int i = 1/0;  編譯能夠通過,但是會拋出異常是程序無法運行,ArithmeticException,當出現異常的運算條件時,拋出此異常。例如,一個整數“除以零”時,拋出此類的一個實例。 
		Computer c  = null;
		if(c!=null)    c.start();   //對象是null,並且調用了對象方法或屬性,就會造成空指針異常。
		String str = "1234abcf"; 
		Integer i = new Integer(str);		 //NumberFormatException
		try{
		Thread.sleep(3000);
		}catch(Exception e){
			e.printStackTrace();    //打印錯誤堆棧軌跡
		}finally{
			System.out.println("aaa");
		}
		//異常處理辦法之三的示例代碼:
		File f = new File("c:/tt.txt");
		if (!f.exists())  {
			try {
				throw new FileNotFoundException("File can't be found!"); 
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
	}
}
class Computer{
	void start(){
		System.out.println("計算機啓動!");
	}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import jdk.nashorn.internal.ir.CatchNode;
//聲明異常,把異常拋給調用他的方法,可一直往上拋,一直拋到最上層。
public class TestReadFile {
	public static void main(String[]  args) throws FileNotFoundException, IOException {
		String str;
		str = new TestReadFile().openFile();
		System.out.println(str);
	} 
	String openFile() throws FileNotFoundException,IOException { 
		FileReader reader = new FileReader("d:/a.txt");
		char c = (char)reader.read();
		System.out.println(c);
		return ""+c;
	}
}
這裏存在一個方法重寫是需要注意的一個地方:

示例代碼:

<span style="font-size:14px;">import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
	class A {	
		public void method() throws IOException {	
			
		}
	}
	
	class B extends A {		
		public void method() throws FileNotFoundException {	
			
		}
	}
	
	class C extends A {		
		public void method() {	
			
		}
	}
	
//	class D extends A {		
	//public void method() throws Exception {	
		
	//}     //超過父類異常的範圍,會報錯!
//	}
	
	class E extends A {		
		public void method() throws IOException, FileNotFoundException {    
			
		}
	}
	class F extends A {		
		public void method() throws IOException, ArithmeticException {      
			
		}
	}
//	class G extends A {		
	//public void method() throws IOException, ParseException {	
		//ParseException超出了父類異常的範圍。
	//}
//	}
</span>
最後介紹的內容是自定義異常:

自定義異常主要是繼承現有的異常類來編寫一個自己定義的子類,加上兩個構造器,下面是實例代碼:

<span style="font-size:14px;">public class MyException extends Exception {
	public MyException(){
		
	}
	
	public MyException(String message){
		super(message);  //仿照JDK裏面的源碼寫的
	}
	
}

class TestMyException{
	void test()throws MyException{
		///
	}
	public static void main(String[] args) {
		try {
			new TestMyException().test();     //new一個這個類的對象,然後調用會拋出異常的方法。最後用try—catch來處理
		} catch (MyException e) {
			e.printStackTrace();
		}
	}
}
</span>
下面是關於異常的總結:



(上述第3點的意思是要先處理子類的異常再去處理父類的異常,否則會發生覆蓋,使得子類的異常始終得不到解決)






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章