Python でカスタム エラー例外を作成しようとしています。引数が辞書にない場合にエラーが発生するようにします_fetch_currencies()
。
カスタム エラー:
class CurrencyDoesntExistError:
def __getcurr__(self):
try:
return _fetch_currencies()[self]
except KeyError:
raise CurrencyDoesntExistError()
関数にどのように書き込んだか:
def convert(amount, from_curr, to_curr, date=str(datetime.date.today())):
"""
Returns the value obtained by converting the amount 'amount' of the
currency 'from_curr' to the currency 'to_curr' on date 'date'. If date is
not given, it defaults the current date.
"""
try:
from_value = float(get_exrates(date)[from_curr])
to_value = float(get_exrates(date)[to_curr])
C = amount * (to_value / from_value)
return C
except CurrencyDoesntExistError:
print('Currency does not exist')
現在、次のエラー メッセージが表示されます。
TypeError: catching classes that do not inherit from BaseException is not allowed
except KeyError:
関数で使用convert
すると実行されますが、このカスタムエラー例外を発生させる正しい方法は何ですか?