-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
Python uses try...except blocks to handle errors gracefully. Instead of crashing the program, you can catch and manage errors during execution.
try:
x = 10 / 0
except:
print("An error occurred")
✅ Output: An error occurred
This prevents the program from crashing due to division by zero.
You can catch specific types of errors like ZeroDivisionError
, ValueError
, etc.
try:
num = int("abc")
except ValueError:
print("Invalid input")
Handle different exceptions separately:
try:
x = int("10")
y = 10 / 0
except ValueError:
print("Invalid conversion")
except ZeroDivisionError:
print("Can't divide by zero")
The else
block runs if no exception occurs.
try:
x = 5 + 3
except:
print("Error")
else:
print("No error occurred")
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")
Use raise
to trigger an exception manually.
x = -5
if x < 0:
raise ValueError("Negative value not allowed")
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.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms