-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert-and-raise.py
More file actions
64 lines (53 loc) · 2.67 KB
/
Copy pathassert-and-raise.py
File metadata and controls
64 lines (53 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# --------------------------------------------------
# assert と raise の違いを確認するサンプルコード
# --------------------------------------------------
# 【概要】
# - assert: 開発時の「デバッグ用・内部前提条件の検証」。
# 条件が False の場合に AssertionError を送出する。
# ※ python -O (最適化モード) で実行すると無視されるため、
# プロダクションコードでのユーザー入力チェック等に使ってはいけない。
#
# - raise : 「本番運用・仕様上のエラー処理(通常の例外処理)」。
# ValueError や TypeError など、状況に応じた明示的な例外を送出する。
# 最適化モードでも無視されず、常に確実に実行される。
# --------------------------------------------------
def divide_with_raise(a: int, b: int) -> float:
"""
raise を使った入力検証の例
通常のアプリケーション仕様として「0での割り算」や「不正な型」を制限する
"""
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
raise TypeError("Arguments must be numbers (int or float).")
if b == 0:
raise ValueError("Division by zero is not allowed.")
return a / b
def process_scores_with_assert(scores: list):
"""
assert を使った内部前提条件チェックの例
「関数内部のロジック上、scores は絶対に空リストではないはずだ」という内部状態を検証
"""
# 内部前提条件の検証(プログラムのバグを早期発見するためのもの)
assert len(scores) > 0, "Programming Error: scores list is empty (internal bug)."
total = sum(scores)
return total / len(scores)
if __name__ == "__main__":
print("=== 1. raise Exception Check ===")
try:
result = divide_with_raise(10, 0)
except ValueError as e:
print(f"[Caught] raise Exception:\n -> {type(e).__name__}: {e}")
try:
result = divide_with_raise(10, "5")
except TypeError as e:
print(f"[Caught] raise Exception:\n -> {type(e).__name__}: {e}")
print("\n" + "=" * 40 + "\n")
print("=== 2. assert AssertionError Check ===")
try:
empty_list = []
average = process_scores_with_assert(empty_list)
except AssertionError as e:
print(f"[Caught] assert Exception:\n -> {type(e).__name__}: {e}")
print("\n" + "=" * 40 + "\n")
print("=== Summary ===")
print("- raise : Standard error handling (user input errors, runtime validation).")
print("- assert: Developer debugging check (verify internal invariants/bugs).")