⚡0 XP🔥0

Ch 3 · Exception Handling | अपवाद प्रबंधन

Lesson 3 of 10 ⏱ 3 min read ✎ 1 exercises ❓ 5-question quiz ▶ Runs Python

Syntax errors vs exceptions, built-in exceptions, raise and assert, and handling exceptions with try, except, else and finally.

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#

▶ Try it YourselfPython
Ctrl + Enter to run

raise and assert#

raise throws an exception deliberately; assert checks a condition and raises AssertionError if it is false.

▶ Try it YourselfPython
Ctrl + Enter to run

Exam tips#

  • Order of blocks: try → except (one or more) → else → finally.
  • finally is used to release resources (close files, connections).

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Safe division

Read two integers (one per line). Print their integer quotient a // b. If b is zero, print Division by zero instead.

Test input:
9
0
Expected output:
Division by zero

Quick Quiz

Q1.

Which block always executes?

Q2.

int('abc') raises

Q3.

The else block runs when

Q4.

Accessing L[10] in a 3-element list raises

Q5.

Keyword used to throw an exception manually is