Python Try....Except


Python uses try...except blocks to handle errors gracefully. Instead of crashing the program, you can catch and manage errors during execution.


🔹 Basic Try...Except

try:
    x = 10 / 0
except:
    print("An error occurred")

✅ Output: An error occurred
This prevents the program from crashing due to division by zero.


🔹 Catch Specific Exception

You can catch specific types of errors like ZeroDivisionError, ValueError, etc.

try:
    num = int("abc")
except ValueError:
    print("Invalid input")

🔹 Multiple Except Blocks

Handle different exceptions separately:

try:
    x = int("10")
    y = 10 / 0
except ValueError:
    print("Invalid conversion")
except ZeroDivisionError:
    print("Can't divide by zero")

🔹 Try...Except...Else

The else block runs if no exception occurs.

try:
    x = 5 + 3
except:
    print("Error")
else:
    print("No error occurred")

🔹 Try...Except...Finally

The finally block always runs — whether there's an error or not.

try:
    f = open("data.txt")
except:
    print("File not found")
finally:
    print("Done")

🔹 Raise an Exception

Use raise to trigger an exception manually.

x = -5
if x < 0:
    raise ValueError("Negative value not allowed")

Practice Questions

Q1. Write a Python program to catch a ZeroDivisionError and print a friendly message.

Q2. Write a Python program to use try...except to handle input that can’t be converted to an integer.

Q3. Write a Python program to open a missing file and handle the FileNotFoundError.

Q4. Write a Python program to create a function that raises a ValueError when the input is negative.

Q5. Write a Python program to use try...except...else to show a message when no error occurs.

Q6. Write a Python program to print "Always runs" using a finally block.

Q7. Write a Python program to catch both IndexError and KeyError in separate except blocks.

Q8. Write a Python program to use raise to stop execution if a variable is empty.

Q9. Write a Python program to nest try-except blocks and handle errors at both levels.

Q10. Write a Python program to use a try block to take input and a finally block to always close the process or display a message.


Python Try....Except Quiz

Q1: What is the purpose of try...except in Python?

A. To define a loop
B. To handle errors
C. To format output
D. To declare a variable

Q2: Which keyword is used to catch exceptions?

A. catch
B. handle
C. except
D. raise

Q3: What happens if an exception occurs and there's no except block?

A. Program runs normally
B. Program crashes
C. Error is ignored
D. Program pauses

Q4: Which block always executes, whether an error occurs or not?

A. try
B. except
C. else
D. finally

Q5: What does the else block in try...except do?

A. Runs if an exception occurs
B. Runs if no exception occurs
C. Runs always
D. Ignores error

Q6: What exception is raised for invalid integer conversion?

A. TypeError
B. ValueError
C. ImportError
D. IndexError

Q7: Which keyword is used to manually trigger an exception?

A. catch
B. except
C. raise
D. throw

Q8: What does ZeroDivisionError mean?

A. You used a negative number
B. You tried dividing by zero
C. File not found
D. List is empty

Q9: Can you have multiple except blocks in Python?

A. Yes
B. No
C. Only if using try inside a function
D. Only one per try block is allowed

Q10: Which block is optional in try...except structure?

A. try
B. except
C. else
D. All required

Go Back Top