Exception Handling: try/except/finally
এক্সসেপশন হ্যান্ডলিং — সুন্দরভাবে fail করা
1. Errors Are Expected
Real programs talk to networks, disks, users, and time — all of which fail. Python uses exceptions to signal "something unexpected just happened." If uncaught, the program crashes with a traceback. Your job is to catch the ones you can meaningfully handle and let the rest propagate upward — loudly.
2. The try / except Block
def safe_divide(a, b):
try:
return a / b
except ZeroDivisionError:
return float("inf")
except TypeError as e:
print(f"bad types: {e}")
return None
print(safe_divide(10, 2))
print(safe_divide(10, 0))
print(safe_divide("10", 2))
try block-এর code-এ exception হলে Python মিলে যাওয়া প্রথম except block-এ ঢোকে। কখনও "bare except" (except:) লিখবেন না — এটি keyboard interrupt-সহ সব কিছু চুপচাপ গিলে ফেলে।
3. The Exception Hierarchy
Every exception is a class, and they form a tree rooted at BaseException. Most user
code should catch Exception or a more specific subclass — never BaseException.
BaseException-এ rooted একটি গাছ তৈরি করে। সাধারণ কোড-এ Exception বা তার subclass catch করুন — BaseException কখনো নয়।
4. else and finally
else runs only if no exception occurred; finally runs always —
success, exception, or return — perfect for cleanup.
else block চলে শুধু তখনই যখন কোনো exception হয়নি। finally block সবসময় চলে — success, exception বা return, সবক্ষেত্রেই। Cleanup-এর জন্য উপযুক্ত।
def parse_int(s):
try:
n = int(s)
except ValueError:
print(f"not a number: {s!r}")
return None
else:
print(f"parsed {n}")
return n
finally:
print("— parse attempt done —")
parse_int("42")
parse_int("hello")
5. Raising and Custom Exceptions
Use raise to signal your own errors. Create a tiny subclass of Exception
when the built-in names don't capture your domain.
raise ব্যবহার করুন। Built-in exception যথেষ্ট না হলে Exception-এর একটি ছোট subclass তৈরি করুন।
class InvalidAgeError(Exception):
"""Raised when an age is outside the allowed range."""
def set_age(age):
if not isinstance(age, int):
raise TypeError("age must be an int")
if age < 0 or age > 150:
raise InvalidAgeError(f"age {age} is unrealistic")
return age
for a in [25, -3, 200]:
try:
print("ok", set_age(a))
except InvalidAgeError as e:
print("rejected:", e)
6. EAFP vs LBYL
✅ EAFP — Easier to Ask Forgiveness (Pythonic)
- Try the operation, handle the error if it fails
- No race conditions between check and action
try: d["x"] except KeyError: ...
⚠️ LBYL — Look Before You Leap
- Check preconditions, then act
- State may change between check and action (race)
if "x" in d: d["x"]
7. Vocabulary
| Term | Meaning | বাংলায় |
|---|---|---|
| Exception | Object raised to signal an abnormal condition. | অস্বাভাবিক পরিস্থিতি জানাতে raise করা object। |
| Traceback | The printed stack of where an exception came from. | Exception কোথা থেকে এসেছে তার stack প্রিন্ট। |
raise | Manually trigger an exception. | Manually একটি exception ঘটায়। |
| Re-raise | Catch, do something, then raise with no argument. | Catch করে কিছু করে আবার raise করা। |
| EAFP | Try first, handle failure. | প্রথমে চেষ্টা, fail হলে handle। |
| LBYL | Check first, then act. | আগে check, পরে action। |
8. Practice Problems
-
Wrap
int(input())-like parsing of a variable intry/except. Use the string"42x".একটি variable-এরint()parsing কেtry/except-এ wrap করুন (string:"42x")।✨ Show Answer
ans1.pyraw = "42x" try: n = int(raw) print("ok:", n) except ValueError as e: print("could not parse:", e) -
Write a
NegativeNumberErrorcustom exception and asqrt_safe(x)that raises it for negative input.কাস্টমNegativeNumberErrorতৈরি করুন এবংsqrt_safe(x)লিখুন যা নেগেটিভ x-এ সেটি raise করবে।✨ Show Answer
ans2.pyclass NegativeNumberError(Exception): pass def sqrt_safe(x): if x < 0: raise NegativeNumberError(f"cannot sqrt {x}") return x ** 0.5 for v in [9, -4]: try: print(sqrt_safe(v)) except NegativeNumberError as e: print("error:", e) -
Why should you avoid a bare
except:clause?Bareexcept:কেন এড়াতে হবে?✨ Show Answer
Answer: It catches everything — including
KeyboardInterruptandSystemExit— which makes programs impossible to stop with Ctrl+C and hides bugs you would have wanted to see. CatchExceptionat most, preferably a specific subclass.এটি
KeyboardInterrupt-সহ সবকিছু catch করে, ফলে Ctrl+C-তেও প্রোগ্রাম বন্ধ হয় না এবং বাগ লুকিয়ে যায়। নির্দিষ্ট class ধরুন, বেশির ভাগ ক্ষেত্রেException। -
Use
try/finallyto ensure a "closing resource" message prints even when an exception occurs.try/finallyদিয়ে নিশ্চিত করুন যে exception হলেও একটি "closing resource" বার্তা প্রিন্ট হবে।✨ Show Answer
ans4.pydef work(fail): try: if fail: raise RuntimeError("boom") print("did work") finally: print("closing resource") work(False) try: work(True) except RuntimeError as e: print("caught:", e) -
Explain in one sentence what the EAFP principle is.এক বাক্যে EAFP নীতি ব্যাখ্যা করুন।
✨ Show Answer
Answer: Easier to Ask Forgiveness than Permission — try the action first and handle the exception if it fails, rather than checking every precondition up-front.
Easier to Ask Forgiveness than Permission — আগে চেক না করে সরাসরি চেষ্টা করুন; fail হলে exception catch করে ব্যবস্থা নিন।
Summary — Module 24
try runs risky code; except catches specific exceptions; else
runs on success; finally runs always. Exceptions form a class hierarchy — catch
narrowly. Raise your own subclasses of Exception when the domain calls for it. Follow
the Pythonic EAFP style: try first, handle failure second.
try-এ ঝুঁকিপূর্ণ code, except-এ নির্দিষ্ট exception, else সফল হলে, finally সবসময়। Exception-গুলো class hierarchy তৈরি করে। প্রয়োজনে নিজের subclass তৈরি করুন এবং EAFP নীতি অনুসরণ করুন।