0% found this document useful (0 votes)
15 views3 pages

Chapter-3 Error and Exception Handling

Uploaded by

Sahil Mhaske
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Chapter-3 Error and Exception Handling

Uploaded by

Sahil Mhaske
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Intermediate

title

Chapter-3 Error and Exception Handling


EOFError: raised when the input() function meets the end-of-file condition.

AttributeError: raised when the attribute assignment or reference fails.

TabError: raised when the indentations consist of inconsistent tabs or


spaces.

ImportError: raised when importing the module fails.

IndexError: occurs when the index of a sequence is out of range

KeyboardInterrupt: raised when the user inputs interrupt keys (Ctrl + C or


Delete).

RuntimeError: occurs when an error does not fall into any category.

NameError: raised when a variable is not found in the local or global scope.

MemoryError: raised when programs run out of memory.

ValueError: occurs when the operation or function receives an argument


with the right type but the wrong value.

ZeroDivisionError: raised when you divide a value or variable with zero.

SyntaxError: raised by the parser when the Python syntax is wrong.

IndentationError: occurs when there is a wrong indentation.

SystemError: raised when the interpreter detects an internal error.

num = int(input("Enter numerator:")) deno = int(input("Enter denominator:")) try: quo =


num/deno print('Quotient:',quo) except ZeroDivisionError: print('Denominator cannot be
Zero')

Multiple excepts
In [6]: try:
# Code that might raise an exception
result = int('a') # This will raise a ValueError
except ValueError:
# Code to handle the ValueError
print("Error: Could not convert to integer!")
except Exception as e:
# Code to handle other exceptions
print("An error occurred:", e)

Error: Could not convert to integer!

Use of Else
In [8]: try:
# Code that might raise an exception
result = 10 / 2
except ZeroDivisionError:
# Code to handle the exception
print("Error: Division by zero!")
else:
# Code to execute if no exceptions are raised
print("Result:", result)

Result: 5.0

Use of finally
In [10]: try:
# Code that might raise an exception
result = 10 / 2
except ZeroDivisionError:
# Code to handle the exception
print("Error: Division by zero!")
else:
# Code to execute if no exceptions are raised
print("Result:", result)
finally:
# Code that always runs
print("This will always execute, regardless of exceptions.")

Result: 5.0
This will always execute, regardless of exceptions.

Raising an Error
In [12]: x = -5

if x < 0:
raise ValueError("Number must be positive")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[12], line 4
1 x = -5
3 if x < 0:
----> 4 raise ValueError("Number must be positive")

ValueError: Number must be positive

In [ ]:

You might also like