0% found this document useful (0 votes)
3 views

exception12

Uploaded by

Ayush Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

exception12

Uploaded by

Ayush Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exception handling

Errors are problems in a program due to which the program will stop the
execution.
Different types of exceptions in python:
 SyntaxError: This exception is raised when the interpreter encounters a
syntax error in the code, such as a misspelled keyword, a missing colon,
or an unbalanced parenthesis.
 TypeError: This exception is raised when an operation or function is
applied to an object of the wrong type, such as adding a string to an
integer.
 NameError: This exception is raised when a variable or function name is
not found in the current scope.
 IndexError: This exception is raised when an index is out of range for a list,
tuple, or other sequence types.
 KeyError: This exception is raised when a key is not found in a dictionary.
 ValueError: This exception is raised when a function or method is called
with an invalid argument or input, such as trying to convert a string to an
integer when the string does not represent a valid integer.
 AttributeError: This exception is raised when an attribute or method is not
found on an object, such as trying to access a non-existent attribute of a
class instance.
 IOError: This exception is raised when an I/O operation, such as reading or
writing a file, fails due to an input/output error.
 ZeroDivisionError: This exception is raised when an attempt is made to
divide a number by zero.
 ImportError: This exception is raised when an import statement fails to find
or load a module.
These are just a few examples of the many types of exceptions that can
occur in Python. It’s important to handle exceptions properly in your code
using try-except blocks or other error-handling techniques, in order to
gracefully handle errors and prevent the program from crashing.
Difference between Syntax Error and Exceptions
Syntax Error: As the name suggests this error is caused by the wrong syntax
in the code. It leads to the termination of the program.
Example:
There is a syntax error in the code . The ‘if' statement should be followed
by a colon (:), and the ‘print' statement should be indented to be inside
the ‘if'
block.
amount = 10000
if(amount > 2999)
print("You are eligible to purchase Dsa Self Paced")
Exceptions: Exceptions are raised when the program is syntactically
correct, but the code results in an error. This error does not stop the
execution of the program, however, it changes the normal flow of the
program.
Example:
Here in this code a s we are dividing the ‘marks’ by zero so a error will occur
known as ‘ZeroDivisionError’
marks = 10000
a = marks / 0
print(a)
In the above example raised the ZeroDivisionError as we are trying to divide
a number by 0.
Note: Exception is the base class for all the exceptions in Python. You can
check the exception hierarchy here.
Example:
1) TypeError: This exception is raised when an operation or function is
applied to an object of the wrong type. Here’s an example:
Here a ‘TypeError’ is raised as both the datatypes are different which are
being added.
x = 5
y = "hello"
z = x + y
try catch block to resolve it:
The code attempts to add an integer (‘x') and a string (‘y') together, which is
not a valid operation, and it will raise a ‘TypeError'. The code used
a ‘try' and ‘except' block to catch this exception and print an error
message.
x= 5
y = "hello"
try:
z = x + y
except TypeError:
print("Error: cannot add an int and a str")
Try and Except Statement – Catching Exceptions
Try and except statements are used to catch and handle exceptions in
Python. Statements that can raise exceptions are kept inside the try clause
and the statements that handle the exception are written inside except
clause.
Example: Here we are trying to access the array element whose index is out
of bound and handle the corresponding exception.
 Python3
a = [1, 2, 3]

try:

print ("Second element = %d" %(a[1]))

print ("Fourth element = %d" %(a[3]))

except:

print ("An error occurred")

You might also like