1.按照程度來分
1.error嚴重錯誤
2.exception一般問題
2.按照處理方式來分
1.捕獲1
) 受檢異常(checked) 在程式中必須接受檢查和處理的異常. 如果不處理編譯錯誤,也稱為編譯時異常
exception及其子類(runtimeexception及其子類除外)
2) 非受檢異常(unchecked) 在程式中不是必須接受檢查和處理的異常, 如果不處理不生編譯錯誤, 但是執行時仍會出問題, 也稱為執行時異常
error及其子類.
runtimeexception及其子類.
2.丟擲:使用throws體現異常的丟擲try
catch
(可能的異常型別1 引用)
catch
(可能的異常型別2 引用)..
...catch
(exception e)
finally
trycatch
trycatch
finally
tryfinally
3.先捕獲再丟擲在方法宣告中的throws 異常型別列表, 作用是警告呼叫者, 此方法有風險.
在方法體中使用throw語句, 在方法中真的丟擲異常物件.
可以只有throws而沒有throw
只要有throw必須要有throws
throws 是警告
throw 是玩真的.
方法覆蓋條件中對於異常的描述 :
要求如下 :
1) 方法簽名一致, 子類方法的返回值的物件型別可以小於等於父類方法返回的物件型別.
2) 子類方法的訪問控制修飾符的範圍要大於等於父類的.
3) 被覆蓋方法不可以被private
,static
,final.4
) 子類方法丟擲的受檢異常範圍要小於等於父類方法丟擲的受檢異常.
先
trycatch
(任意異常型別 引用)
處理方式的選擇 :
1) 入口方法盡量捕獲.
2) 功能方法盡量丟擲.
自定義異常 : 1) 繼承exception 表明它是乙個異常,
2) 提供2個構造器乙個string,乙個是throwable 建立自定義異常物件方便
class
dividedbyzeroexception
extends
exception
public
dividedbyzeroexception
(throwable cause)
}public
class
exceptiontest
catch
(arithmeticexception e)
}public
static
void
main
(string[
] args)
catch
(dividedbyzeroexception e)
system.out.
println
(divide(20
,5))
;}catch
(dividedbyzeroexception e)
finally}}
class
exceptiontest3
return x / y;
}//public static void main(string args) throws dividedbyzeroexception
catch
(dividedbyzeroexception e)
system.out.
println
("end");
}}class
exceptiontest2
return x / y;
}public
static
void
main
(string[
] args)
catch
(runtimeexception e)
system.out.
println
("end...");
}}class
exceptiontest1
trycatch
(arrayindexoutofbound***ception e)
catch
(numberformatexception e)
catch
(exception e)
finally
system.out.
println
("main end");
}}
string 類的例項表示乙個字串,string 是一種不可變字串,一對字串進行改變操作,則建立乙個全新的物件,然後把引用指向這個新的物件。
*
public
intlength()
. string.
length()
=>
35 獲取字串長度(字元數)
*public
char
charat
(int index) 獲取指定下標位置處的字元 string.
charat(12
)=>
'歡', string.
charat(18
)=>
'我';
*public
char
tochararray
() 獲取內部的char
*char result=
newchar
[value.length]
;// 建立乙個新陣列, 容量和內部的value一樣大.**
for(
int i =
0; i < value.length; i++)*
* system.
arraycopy
(value,
0, result,
0, value.length);*
// 第乙個引數是源陣列物件, 第二個引數是源陣列的開始下標, 第三個引數是目標陣列物件, 第四個引數是目標陣列的開始下標
*// 第五個引數是要複製的元素個數.
public
class
stringtest
string string2 =
newstring
(arr)
; system.out.
println
(string2);}
@test
public
void
exer12()
system.out.
println
(string2);}
@test
public
void
exer1()
system.out.
println
(string2);}
@test
public
void
test5()
system.out.
println
("***************************");
char
chars = string.
tochararray()
;for
(int i =
0; i < chars.length; i++)}
@test
public
void
test4()
; string s1 =
newstring
(arr)
;// 製作乙個副本
arr[1]
='大'
; system.out.
println
(s1)
; string s2 =
newstring
(arr,2,
3);// 第2個引數是開始下標, 第3個引數是長度
system.out.
println
(s2)
; string s3 =
newstring
(arr,
0, arr.length)
; system.out.
println
(s3);}
@test
public
void
test3()
@test
public
void
test2()
@test
public
void
test1()
}
C 異常學習筆記
vc 中的異常捕獲的規則 可以捕獲的異常有以下三種情況 1.必須嚴格遵守匹配資料型別 2.異常處理的資料型別是公有類,拋擲的異常的資料型別是派生類。3.異常處理的資料型別是指向公有基類的指標,拋擲異常的資料型別是指向派生類的執針 對於派生層次結構的異常處理,catch 塊組的順序是重要的。因為 ca...
java學習筆記 異常
1 異常的限制 當覆蓋方法的時候,只能丟擲在基類方法的異常說明裡列出的那些異常。這個限制很有用,因為這意味著,當基類使用的 應用到期派生類物件的時候,一樣能夠工作。public class exceptionrestrictiontest extends inning implements stor...
Python 異常 學習筆記
python 標準異常總結 try 和 try finally 語句 try 下面是檢測範圍,如發生異常停止在第乙個異常的位置 fh open testfile w fh.write 這是乙個測試檔案,用於測試異常 except oserror as reason 下面是出現異常後輸出的 print...