#include
using
namespace std;
//定義除數為0異常類
class
zeroexception
char
*show()
};//定義總分或科目數為負數異常類
class
negativeexception
char
*show()
};float
div(
float score,
int n)
if(n<
0||score<0)
//丟擲輸入為負異常
return score/n;
}int
main()
catch
(zeroexception a)
//捕獲除數為0異常
//這裡說明型別的同時也宣告了類的例項物件a
catch
(negativeexception b)
}return0;
}
執行結果:
異常類的派生
C 異常處理類與自定義異常處理類
例1 自定義乙個繼承自excepton的異常類myexception c 標準中,定義在中的任何異常類都派生自exception class,本例也只是簡單地由exception繼承,在try段丟擲乙個異常並捕捉。如下 test.cpp version 1.0 decript define a ex...
Python學習 異常處理,自定義異常類
usr bin python coding utf 8 filename usingexception.py 異常處理 寫乙個自己定義的異常類 class myinputexception exception def init self,length,least exception.init sel...
異常處理 自定義異常
異常 1.定義 執行時檢測到的錯誤。2.現象 當異常發生時,程式不會再向下執行,而轉到函式的呼叫語句。3.常見異常型別 名稱異常 nameerror 變數未定義。型別異常 typeerror 不同型別資料進行運算。索引異常 indexerror 超出索引範圍。屬性異常 attributeerror ...