Handle errors with care
try runs code that may fail. except catches a specific error so your program can respond. Catch only errors you know how to handle. finally runs cleanup whether or not an error happens.
हिन्दी में समझें
try में ऐसा कोड लिखें जो विफल हो सकता है। except खास त्रुटि सँभालता है। केवल समझी हुई त्रुटि पकड़ें। finally सफाई का काम हर स्थिति में करता है।
अभ्यास: सही टेक्स्ट "42" बदलें और संख्या प्रिंट करें। त्रुटि प्रबंधन रखें।
Hinglish mein samjhein
try mein fail ho sakne wala code rakho. except specific error handle karta hai. Sirf wahi errors pakdo jo handle karna samajhte ho. finally cleanup ke liye hai.
Practice: Valid text "42" convert karke print karo. Error handling rakho.
A small Python example
try:
number = int("hello")
except ValueError:
print("Please use a number.")The browser playground runs real Python with Pyodide. Use sample variables instead of interactive input(). Browser package support differs from desktop Python.
Try this example in the Python playground →Practise this idea
Convert the valid text "42" and print the resulting number. Keep error handling.
Check answer & explanation
try:
number = int("42")
except ValueError:
print("Please use a number.")
print(number)"42" is valid integer text, so conversion succeeds. The except branch remains available for invalid text.
Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.
Check your understanding
Which error does int("hello") raise?
Check the quiz answer
ValueError
The text cannot be interpreted as an integer.
References & corrections
Use the official Python documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.