1.1exception Handling
1.1exception Handling
1. What is an Error?
Types of Errors:
1. Syntax Errors
2. Syntax Errors
• Errors in the structure of the code (spelling, missing colons, brackets, etc.).
Example:
Output:
• Happen due to invalid actions like dividing by zero or using wrong data types.
Example:
a=5/0
Output:
Vaibhav Chaudhary
PGT Computer Science
Unit -1 Chapter 1
5. Types of Exceptions
ZeroDivisionError Division by 0 10 / 0
Vaibhav Chaudhary
PGT Computer Science
Unit -1 Chapter 1
User-Defined Exceptions
You can create your own exceptions by defining a class that inherits from Python's Exception
class.
Example 1:
class NegativeNumberError(Exception):
pass
number = -5
if number < 0:
Example 2:
class TooShortError(Exception):
pass
name = "Al"
if len(name) < 3:
Without Handling:
With Handling:
Vaibhav Chaudhary
PGT Computer Science
Unit -1 Chapter 1
Throwing an Exception
Example:
age = -1
if age < 0:
Catching an Exception
8. Raising Exceptions
a. raise Statement
Example:
marks = -50
if marks < 0:
b. assert Statement
Used to check if a condition is true. If condition with assert statement is true then exception is
not raised, program executes normally but the condition with assert is false, it raises an
AssertionError.
Example:
age = -3
Vaibhav Chaudhary
PGT Computer Science
Unit -1 Chapter 1
9. Handling Exceptions
Python uses the try-except block to catch and fix errors without crashing.
10. Catching Exceptions (Handling the exception:- use try except clause)
Try block:-
We write those statements in try block in which there is possibility of having /getting exceptions.
If any type of exception occurred in code written in try block during program execution then
program control goes to the corresponding except block written to handle that particular type of
exception.
Syntax:
try:
# risky code
except ExceptionType:
Except Block:-
Except block of a particular type is executed when an exception of that type occurred in try
block . When an exception is thrown in try block , control goes to the except block which
matches that exception . Then code written in except block is executed.
Multiple except block can be created each for different types of possible exceptions which may
be encountered in try block.
Example:
try:
print(10 / x)
except ZeroDivisionError:
Vaibhav Chaudhary
PGT Computer Science
Unit -1 Chapter 1
except ValueError:
• When no exception occurs in try block then no except block executes. Then program
control goes to the else block and code of else block is executed.
Example:
try:
except ValueError:
print("Invalid input!")
else:
print("You entered:", x)
If user enters a valid number value then exception is not raised and code of else block executes.
Example:
try:
f = open("data.txt", "r")
print(f.read())
except FileNotFoundError:
Vaibhav Chaudhary
PGT Computer Science
Unit -1 Chapter 1
finally:
Term Use
Handle the error. Write those statements here which should be executed on
Except
getting exception.
Exception
Base class for user-defined exceptions
class
class AgeError(Exception):
pass
try:
Vaibhav Chaudhary
PGT Computer Science
Unit -1 Chapter 1
except ValueError:
except AssertionError as e:
print("Assertion Error:", e)
except AgeError as e:
print("Custom Error:", e)
else:
print("Age is valid.")
finally:
Vaibhav Chaudhary
PGT Computer Science