一、Java異常處理詳解
Java異常處理-筆記中的@doublefan講解得非常通熟易懂
異常.png
目錄:
1.java中異常的分類
1.1 異常(Exception)
1.1.1 運行時異常(RuntimeException)
可以不需要捕獲
1.1.2 編譯異常(IOException)
編譯器會提示要捕獲,如果不進(jìn)行捕獲則編譯器會報錯
1.2 錯誤(Error)
3.java處理異常機制
4.throw和throws的區(qū)別
5.如何捕獲異常
try-catch-finally
6.不同異常的捕獲要分開處理
二、檢查型異常和非檢查性異常對比
檢查型異常.png
非檢查型異常.png
三、throws和throw的區(qū)別 以及 throws、throw和try、catch的對比
Java異常處理-筆記中的@Husky講解得非常通熟易懂
以下代碼來自@Husky
class FuShuException extends Exception //getMessage();
{
private int value;
FuShuException()
{
super();
}
FuShuException(String msg,int value)
{
super(msg);
this.value = value;
}
public int getValue()
{
return value;
}
}
class Demo
{
//通過throws 和 throw捕獲異常
int div(int a,int b)throws FuShuException
{
if(b<0) {
// 手動通過throw關(guān)鍵字拋出一個自定義異常對象。
throw new FuShuException("出現(xiàn)了除數(shù)是負(fù)數(shù)的情況------ / by fushu",b);
}
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String[] args)
{
Demo d = new Demo();
//通過try-catch捕獲異常
try
{
int x = d.div(4,-9);
System.out.println("x="+x);
}
catch (FuShuException e)
{
System.out.println(e.toString());
//System.out.println("除數(shù)出現(xiàn)負(fù)數(shù)了");
System.out.println("錯誤的負(fù)數(shù)是:"+e.getValue());
}
System.out.println("over");
}
}