Lesson 28 of 34

try / except: Errors हाताळणे

9 min 30 XP

वापरकर्त्याने संख्येऐवजी अक्षरे टाइप केली तर int() ValueError देते आणि प्रोग्राम थांबतो. धोकादायक कोड try मध्ये ठेवा आणि समस्या except मध्ये हाताळा.

Example: edit me and press Run

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

प्रत्येक प्रकारच्या error साठी वेगळा except लिहा. finally भाग नेहमी चालतो.

Example: edit me and press Run

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

तुम्ही स्वतःच्या errors उभ्या करू शकता: raise ValueError("Age cannot be negative")

Challenge

+30 XP

input() ने एक value वाचा. पूर्ण संख्या असल्यास तिची दुप्पट print करा. नाहीतर Invalid print करा. try / except वापरा.

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