Lesson 28 of 34

try / except: பிழைகளைக் கையாளுதல்try / except: Handling Errors

9 min 30 XP

பயனர் எண்ணுக்குப் பதிலாக எழுத்தை type செய்தால் int() பிழை (ValueError) தரும், program நின்றுவிடும். try உள்ளே ஆபத்தான code-ஐ வைத்து, except-ல் பிழையைச் சமாளிக்கலாம்.

If a user types letters instead of a number, int() raises a ValueError and the program stops. Put risky code in try and handle the problem in except.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

வெவ்வேறு பிழைகளுக்கு வெவ்வேறு except எழுதலாம். finally பகுதி எப்போதும் இயங்கும்.

Write a separate except for each kind of error. The finally part always runs.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

raise மூலம் நீங்களே பிழையை எழுப்பலாம்: raise ValueError("Age cannot be negative")

You can raise your own errors: raise ValueError("Age cannot be negative")

Challenge

+30 XP

ஒரு மதிப்பை input() மூலம் பெறுங்கள். அது ஒரு முழு எண் என்றால் அதன் இரு மடங்கை (× 2) print செய்யுங்கள். இல்லையெனில் Invalid என்று print செய்யுங்கள். try / except பயன்படுத்துங்கள்.

Read a value with input(). If it is a whole number, print double the number. Otherwise print Invalid. Use try / except.

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.