Exception的一些問題

2021-10-07 07:21:01 字數 1509 閱讀 2913

從這個鏈結可以了解到一些基本的exception類

從上面鏈結可以更直觀的了解產生的error樹

模擬資料,來測試下幾種exception:

def

show_exception

(input):

try:

i =0 j =

1 a =

input

[i] b =

input

[j] c = a/b

print

("the result of a/b is :"

,c)except indexerror:

print

("index error occur...the ar** is not enough"

)except arithmeticerror:

print

("arithmeric error occur..."

)except exception as e:

print

("unknown error occur..."

,e)

a =5

b =4

data =

print

("data is :"

,data)

print

("a is :"

,a)print

("b is :"

,b)show_exception(data)

那麼會出現:

data is : [5]

a is : 5

b is : 4

index error occur...the ar** is not enough

2、如果把b改為0:

b =

0

那麼會出現:

data is : [5, 0]

a is : 5

b is : 0

arithmeric error occur...

3、如果把函式中的 j 值改掉

j = 1.5
那麼會出現:

data is : [5, 4]

a is : 5

b is : 4

unknown error occur... list indices must be integers or slices, not float

1、如果能夠判斷會出現哪些exception類,如arithmericerror類等,那麼可以專門為這類異常安排處理邏輯;

2、根據異常樹的順序,優先從樹的下方開始安排專項異常處理邏輯;

3、最後安排樹的根節點exception 類處理邏輯;

4、注意else模組中的異常不會被函式的exception處理給捕獲,會傳遞出去。

String 一些問題

前言 等號 對於基本型別,比較的是值,對於引用型別,比較的是記憶體位址。1.在物件池中建立,如果常量池中已經存在則返回常量池中已經有的。private static void test1 結果 true 2.乙個在string pool中,乙個在堆中。private static void test...

C 一些問題

1 if else語句和switch case語句的效率分析對比 switch效率高。switch的效率與分支數無關,當只有分支比較少的時候,if效率比switch高,因為switch有跳轉表。分支比較多,那當然是switch 根據大量的實際程式測試 不考慮不同的編譯器優化程度差異,假設都是最好的優...

mysql 一些問題

1 中文亂碼 問題 推薦用 uft 8 編碼 適配一切介面,mysql中 發生中文亂碼時 開啟mysql 安裝路徑 更改後 重啟mysql 服務即可,有時也需要 重新匯入資料庫 可能是與 source 檔案時 編碼沒有設定好 client password your password port 33...