【java】異常

異常體系:
Throwable
---Error
一般出現重大問題:如類不存在或者內存溢出。中止運行,不對代碼進行處理
---Exception
在運行時出現的狀況,能夠進行鍼對性處理。能夠經過try catch finallyjava

  ---RuntimeException  運行時異常。函數


異常處理:this

try
{
    須要被檢測的代碼
}
catch (異常類 變量)
{
    處理異常的代碼
}
finally
{
    必定會執行的代碼
  一般是關閉資源或鏈接。 }

 

例子:對象

class Div
{
	private int a;
	private int b;
	public int div(int a,int b)
	{
		this.a=a;
		this.b=b;
		return a/b;
	}

}


class  DivDemo
{
	public static void main(String[] args) 
	{
		try
		{
			Div d1=new Div();
			int x=d1.div(6,0);		
			System.out.println(x);
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());/// by zero
			System.out.println(e.toString());///java.lang.ArithmeticException: / by zero
			e.printStackTrace();//詳細的堆棧信息
								 //java.lang.ArithmeticException: / by zero
								 // at Div.div(Divdemo.java:9)
								 // at DivDemo.main(Divdemo.java:22)
		}
		
	}
}

  

對多異常的處理:
1.聲明異常時,建議聲明更爲具體的異常。這樣處理的能夠具體些
2.對方聲明幾個異常,就對應有幾個catch塊,不要定義多餘的catch塊。
3.若是多個catch塊中的異常出現繼承關係,父類異常必定要放在最下面blog

 

自定義異常:
當函數內部出現了throw拋出異常對象,那麼就必須給對應的處理動做。
1.要麼在內部try catch處理
2.要麼在函數上聲明讓調用者處理
3.如何自定義異常信息:由於父類中已經異常信息的操做都完成了。因此子類只要在構造時,講異常信息傳遞給父類,經過super語句。繼承

 通常狀況,函數內部出現異常,函數上須要聲明接口

4.自定義類必須繼承Exception.
5.throws和throw的區別:
  1)throws 使用在函數上,用於函數聲明;throw 使用在函數內。
  2)throws 後面緊跟着聲明的異常類,能夠多個,用逗號隔開;throw後面跟着是拋出異常類的對象。內存

6.catch 是用於處理異常。若是沒有catch就表明異常沒有被處理過,若是該異常是檢測時異常,那麼必須聲明資源

7.finall一種狀況下不會執行,當程序執行到System.exit(0),finally不會執行。例如:get

class Test
{ 
	public static String output=""; 
	public static void foo(int i)
	{ 
		try
		{ 
			if(i==1)
				throw new Exception(); 	
			output+="1"; 
		} 
		catch(Exception e)
		{ 
			output+="2"; 
			return; //returun後finally繼續執行,可是其餘語句就再也不執行了
		} 
		finally
		{ 
			output+="3"; 
		} 
		output+="4"; 
	}
	public static void main(String args[])
	{ 
		foo(0);
		System.out.println(output);//134
		foo(1); 
		System.out.println(output); //13423
	}
} 

8.throw單獨存在,下面不要定義語句,由於執行不到。

class FushuException extends Exception
{
	FushuException(String msg)
	{
		super(msg);
	}
}

class Div
{
	private int a;
	private int b;
	public int div(int a,int b) throws FushuException//函數內部出現throw拋出異常,函數上須要throws聲明。
	{
		this.a=a;
		this.b=b;
		if (b<0)
		{
			throw new FushuException("出現了負數");
               //System.out.print("dddd");//throw單獨存在,下面不要定義語句,由於執行不到。 } return a/b; } } class FuDemo { public static void main(String[] args) { try { Div D1=new Div(); D1.div(3,-1); } catch (FushuException e) { System.out.print(e.toString()); } } }

 

Exception 中有一個特殊的子類異常叫RuntimeException 運行時異常。特色:出現了沒法運行的狀況,須要對代碼進行修改

1.若是在函數內throw拋出異常,函數上能夠不用聲明,編譯同樣經過。
2.若是在函數上聲明瞭該異常,調用者能夠不用進行處理,編譯同樣經過。

自定義異常時:若是該類的發生,沒法在繼續進行運行,可以讓自定義異常類繼承RuntimeException

異常分類:
1.編譯時被檢測的異常
2.編譯時不被檢測的異常(RuntimeException以及子類)

catch內,須要定義針對性的處理方式。不要簡單的定義printStackTrace,輸出語句。
		也不要不寫。
		當捕獲到的異常,本功能處理不了時,能夠繼續在catch中拋出。
		try
		{
			throw new AException();
		}
		catch (AException e)
		{
			throw e;
		}

		若是該異常處理不了,但並不屬於該功能出現的異常。
		能夠將異常轉換後,在拋出和該功能相關的異常。

		或者異常能夠處理,當須要將異常產生的和本功能相關的問題提供出去,
		當調用者知道。並處理。也能夠將捕獲異常處理後,轉換新的異常。
		try
		{
			throw new AException();
		}
		catch (AException e)
		{
			// 對AException處理。
			throw new BException();
		}

		好比,匯款的例子。

  

異常在子父類覆蓋中的體現:
1.子類在覆蓋父類時,若是父類的方法拋出異常,那麼子類的覆蓋方法,只能拋出父類的異常或者該異常的子類。
2.若是父類方法拋出多個異常,那麼子類在覆蓋該方法時,只能拋出父類異常的子集。
3.若是父類或者接口的方法沒有異常拋出,那麼子類在覆蓋方法時,也不能夠拋出異常。
若是子類方法發生了異常,必須進行try處理,絕對不能拋

class NoValueException extends RuntimeException
{
	NoValueException(String message)
	{
		super(message);
	}
}

interface Shape
{
	void getArea();
}

class Rec implements Shape
{
	private int len,wid;

	Rec(int len ,int wid)//throws NoValueException
	{
		if(len<=0 || wid<=0)
			throw new NoValueException("出現非法值");

		this.len = len;
		this.wid = wid;
	}

	public void getArea()
	{
		System.out.println(len*wid);
	}
}


class Circle implements Shape
{
	private int radius;
	public static final double PI = 3.14;

	Circle(int radius)
	{
		if(radius<=0)
			throw new NoValueException("非法");
		this.radius = radius;
	}

	public void getArea()
	{
		System.out.println(radius*radius*PI);
	}
}




class  ExceptionTest1
{
	public static void main(String[] args) 
	{	
		
		Rec r = new Rec(3,4);
		r.getArea();

		Circle c = new Circle(-8);

		System.out.println("over");
	
	}
}
相關文章
相關標籤/搜索