12/10/24, 1:57 PM Errors and Exceptions.
ipynb - Colab
keyboard_arrow_down Errors and Exceptions in Python
Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some
internal events occur which changes the normal flow of the program. Two types of Error occurs in python.
1. Syntax errors
2. Logical errors (Exceptions)
keyboard_arrow_down Syntax error
When the proper syntax of the language is not followed then a syntax error is thrown.
# initialize the amount variable
amount = 10000
# check that You are eligible to
# purchase Dsa Self Paced or not
if(amount>2999)
print("You are eligible to purchase Dsa Self Paced")
File "<ipython-input-1-84e56fbe4e0d>", line 6
if(amount>2999)
^
SyntaxError: invalid syntax
keyboard_arrow_down Logical errors(Exception)
When in the runtime an error that occurs after passing the syntax test is called exception or logical type. For example, when we divide any
number by zero then the ZeroDivisionError exception is raised, or when we import a module that does not exist then ImportError is raised.
# initialize the amount variable
marks = 10000
# perform division with 0
a = marks / 0
print(a)
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-2-5f312778a383> in <module>
3
4 # perform division with 0
----> 5 a = marks / 0
6 print(a)
ZeroDivisionError: division by zero
if(a<3):
print("gfg")
File "<ipython-input-3-9d7cace61905>", line 2
print("gfg")
^
IndentationError: expected an indented block
https://colab.research.google.com/drive/1aQnLHmgBN8Ngwil96ej3POB4loPGDBii#printMode=true 1/3
12/10/24, 1:57 PM Errors and Exceptions.ipynb - Colab
keyboard_arrow_down Error Handling
When an error and an exception are raised then we handle that with the help of the Handling method.
Handling Exceptions with Try/Except/Finally
We can handle errors by the Try/Except/Finally method. we write unsafe code in the try, fall back code in except and final code in finally block.
# put unsafe operation in try block
try:
print("code start")
# unsafe operation perform
print(1 / 0)
# if error occur the it goes in except block
except:
print("an error occurs")
# final code in finally block
finally:
print("Concluded")
code start
an error occurs
GeeksForGeeks
keyboard_arrow_down Raising exceptions for a predefined condition
When we want to code for the limitation of certain conditions then we can raise an exception.
# try for unsafe code
try:
amount = 1999
if amount < 2999:
# raise the ValueError
raise ValueError("please add money in your account")
else:
print("You are eligible for loan")
# if false then raise the value error
except ValueError as e:
print(e)
https://colab.research.google.com/drive/1aQnLHmgBN8Ngwil96ej3POB4loPGDBii#printMode=true 2/3
12/10/24, 1:57 PM Errors and Exceptions.ipynb - Colab
please add money in your account
age = int(input('Age of voter'))
try:
if (age<18):
raise ValueError('Not Eligible for Age')
else:
print('Eligible')
except ValueError as val:
print(val)
Age of voter18
Eligible
try:
age = int(input("Input your age: "))
if age < 0:
print("Age cannot be negative.")
elif age>17:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
except ValueError:
print("Invalid input. Please input a valid age.")
Input your age: h
Invalid input. Please input a valid age.
Start coding or generate with AI.
https://colab.research.google.com/drive/1aQnLHmgBN8Ngwil96ej3POB4loPGDBii#printMode=true 3/3