What are Exceptions& Why They Occur?
An exception is a runtime error that occurs while the program is running and
stops normal execution.
5.
What are Exceptions& Why They Occur?
An exception is a runtime error that occurs while the program is running and
stops normal execution.
Python raises an exception when:
● Invalid input is given
● Illegal operation is performed
● Resource is unavailable
6.
Lets see withexample :
Example (Without Exception Handling)
a = int(input("Enter a number: "))
b = int(input("Enter another number: "))
print(a / b)
7.
Lets see withexample :
Example (Without Exception Handling)
a = int(input("Enter a number: "))
b = int(input("Enter another number: "))
print(a / b)
If user enters b = 0
Output:
ZeroDivisionError: division by zero
Program crashes → bad user experience
Syntax Error vsRuntime Error
Syntax Error
● Happens before execution
● Python cannot understand the code
Example
if x == 10
print(x)
11.
Syntax Error vsRuntime Error
Syntax Error
● Happens before execution
● Python cannot understand the code
Example
if x == 10
print(x)
👉 Cannot be handled using try-except
12.
Syntax Error vsRuntime Error
Syntax Error
● Happens before execution
● Python cannot understand the code
Example
if x == 10
print(x)
SyntaxError: invalid syntax
Runtime Error (Exception)
● Happens during execution
● Can be handled
Example
print(10 / 0)
ZeroDivisionError
13.
Syntax Error vsRuntime Error
Syntax Error
● Happens before execution
● Python cannot understand the code
Example
if x == 10
print(x)
SyntaxError: invalid syntax
Runtime Error (Exception)
● Happens during execution
● Can be handled
Example
print(10 / 0)
ZeroDivisionError
Syntax errors stop program from starting, runtime errors occur while program is running.
14.
try and exceptBlocks
Why try - except?
To prevent program crash and handle errors gracefully.
Basic Syntax
try:
risky code
except:
handle error
15.
try and exceptBlocks
Why try - except?
To prevent program crash and handle errors gracefully.
Basic Syntax
try:
risky code
except:
handle error
try:
a = int(input("Enter number: "))
b = int(input("Enter number: "))
print(a / b)
except:
print("Something went wrong")
16.
Catching Specific Exceptions
Bad
except:
Example
try:
a= int(input("Enter number: "))
b = int(input("Enter number: "))
print(a / b)
except:
print("Something went wrong")
try:
a = int(input())
b = int(input())
print(a / b)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Please enter numbers only")
● Python checks exceptions top to bottom
● Different messages for different errors
17.
else Clause withtry-except
When does else run?
-> Only if NO exception occurs
Example
try:
num = int(input("Enter number: "))
result = 10 / num
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")
else:
print("Result:", result)
18.
finally Clause
What isfinally?
● Always executes
● Runs whether error occurs or not
● Used for cleanup (files, DB, connections)
● Even if file is missing → finally runs
Example
try:
f = open("data.txt", "r")
print(f.read())
except FileNotFoundError:
print("File not found")
finally:
print("Closing file")
19.
Full Combined Example
try:
a= int(input("Enter a number: "))
b = int(input("Enter another number: "))
result = a / b
except ZeroDivisionError:
print("Division by zero is not allowed")
except ValueError:
print("Enter valid numbers only")
else:
print("Result:", result)
finally:
print("Program execution completed")