Errors and exceptions#
- Syntax errors are found before the program runs (missing colon, wrong indentation).
- Exceptions happen while running:
ZeroDivisionError,ValueError,TypeError,IndexError,KeyError,NameError,FileNotFoundError,IOError,EOFError,ImportError.
सिंटैक्स त्रुटि प्रोग्राम चलने से पहले पकड़ी जाती है, जबकि अपवाद (exception) प्रोग्राम चलते समय होता है - जैसे शून्य से भाग देना। try-except से हम अपवाद को संभालकर प्रोग्राम को बीच में रुकने से बचाते हैं।
try – except – else – finally#
raise and assert#
raise throws an exception deliberately; assert checks a condition and raises AssertionError if it is false.
Exam tips#
- Order of blocks: try → except (one or more) → else → finally.
finallyis used to release resources (close files, connections).