Python · Chapter 2.9 · Pro level
Error Handling: try / except (Programs That Never Crash)
Error Handling: try / except (कभी न रुकने वाले प्रोग्राम)
What if a user types "abc" when you asked for a number? int("abc") crashes with ValueError. Professional programs never crash in front of users — they catch errors. Put risky code inside try: and the rescue plan inside except ValueError:. Think of a safety net under a circus rope-walker: if he falls (error), the net (except) catches him and the show goes on. finally: runs no matter what — like saying 'thank you' at the end.
अगर नंबर माँगने पर यूज़र "abc" लिख दे तो? int("abc") ValueError देकर रुक जाता है। प्रोफ़ेशनल प्रोग्राम यूज़र के सामने कभी crash नहीं होते — वे errors को पकड़ते (catch) हैं। जोखिम वाला कोड try: में और बचाव का प्लान except ValueError: में लिखो। इसे सर्कस में रस्सी पर चलने वाले के नीचे लगे जाल जैसा समझो: अगर वह गिरे (error), तो जाल (except) पकड़ लेता है और शो चलता रहता है। finally: हर हाल में चलता है — आख़िर में 'धन्यवाद' कहने जैसा।
Words and symbols in this chapter
try:- Try the risky code inside this block.
- इस block के अंदर जोखिम वाला कोड आज़माओ।
except ValueError:- Runs only if that type of error happens in
try. - तभी चलता है जब
tryमें उस तरह की error आए। except ZeroDivisionError as e:as estores the error message so you can print it.as eerror का संदेश रख लेता है ताकि print कर सको।else: / finally:elseruns if NO error happened;finallyruns always.elseतब चलता है जब कोई error न आए;finallyहमेशा चलता है।raise ValueError("...")- Create your own error on purpose (e.g. negative age).
- जानबूझकर अपनी error बनाना (जैसे नेगेटिव उम्र)।
Ready example
text = input("Ek number likho: ")
try:
num = int(text)
print("100 /", num, "=", 100 / num)
except ValueError:
print("Arre! Yeh number nahi hai:", text)
except ZeroDivisionError:
print("0 se divide nahi kar sakte!")
finally:
print("Program safe tarike se khatam hua ✅")
Sample input typed into the Input box, one value per line:
abc
What you will see: Try the 📥 Input values abc, 0 and 20 one by one — the program never crashes!
📥 Input में बारी-बारी abc, 0 और 20 लिखकर देखो — प्रोग्राम कभी crash नहीं होगा!
Line by line
try:- Everything indented below is protected.
- नीचे indent किया हुआ सब सुरक्षित है।
num = int(text)- Fails with ValueError if the text is not a number.
- टेक्स्ट नंबर न हो तो ValueError देता है।
except ValueError: / except ZeroDivisionError:- Separate friendly messages for each kind of mistake.
- हर तरह की गलती के लिए अलग प्यारा संदेश।
finally:- Always prints the last line, error or not.
- Error आए या न आए, आख़िरी लाइन हमेशा छपती है।
Common mistake
✗ Wrong
try:
print(int("x"))
except:
pass✓ Right
try:
print(int("x"))
except ValueError:
print("Not a number")Code inside try/except must be indented, and catching a specific error is better than a silent bare except.
try/except के अंदर का कोड indent होना चाहिए, और चुपचाप सब पकड़ने से अच्छा है ख़ास error पकड़ना।
Your task
Protect the code with try: / except ValueError: so typing abc prints a friendly message instead of crashing.
कोड को try: / except ValueError: से सुरक्षित करो ताकि abc लिखने पर crash की जगह प्यारा संदेश छपे।
- Write
try:above the two lines and indent them with 4 spaces.
दोनों लाइनों के ऊपरtry:लिखो और उन्हें 4 स्पेस आगे खिसकाओ। - Below, write
except ValueError:and indentedprint("Sirf number likho!").
नीचेexcept ValueError:और उसके अंदरprint("Sirf number likho!")लिखो। - Keep
abcin the 📥 Input box and Run — no red error now!
📥 Input box मेंabcरहने दो और Run करो — अब लाल error नहीं आएगी!
Starter code for the task
# 📥 Input box mein "abc" likho aur Run karo — program crash hoga!
# TODO: neeche ki 2 lines ko try: ke andar daalo (4 spaces) aur
# except ValueError: mein print("Sirf number likho!") karo
age = int(input("Umar? "))
print("Agle saal:", age + 1)Do this task in the classroom: your code is checked automatically and you get hints if something is missing.
Recap
- Risky code goes in
try:; the rescue goes inexcept ErrorType:. - Catch specific errors (
ValueError,ZeroDivisionError) with friendly messages. else= no error happened,finally= always runs;raisecreates your own error.
- जोखिम वाला कोड
try:में; बचावexcept ErrorType:में। - ख़ास errors (
ValueError,ZeroDivisionError) पकड़कर प्यारे संदेश दिखाओ। else= कोई error नहीं,finally= हमेशा चलता है;raiseअपनी error बनाता है।
Quick quiz
Which block runs only when an error happens inside try?
कौन-सा block तभी चलता है जब try में error आए?
Show the answer
Answer: C (except)
except catches the error. finally always runs; else runs when there is no error.
except error पकड़ता है। finally हमेशा चलता है; else बिना error के चलता है।
Practice problems for this chapter
- Safe ATM Withdrawal (input + try / except / raise / finally)
सुरक्षित ATM निकासी (input + try / except / raise / finally)
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.