0% found this document useful (0 votes)
5 views4 pages

Exception Handling in Python

The document discusses exception handling in Python, explaining how to manage errors that occur during program execution using try, except, and finally blocks. It provides examples of handling specific exceptions like NameError and ZeroDivisionError, as well as demonstrating how to raise exceptions intentionally. Additionally, it highlights the importance of ensuring resources are properly managed, such as closing files after operations.

Uploaded by

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

Exception Handling in Python

The document discusses exception handling in Python, explaining how to manage errors that occur during program execution using try, except, and finally blocks. It provides examples of handling specific exceptions like NameError and ZeroDivisionError, as well as demonstrating how to raise exceptions intentionally. Additionally, it highlights the importance of ensuring resources are properly managed, such as closing files after operations.

Uploaded by

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

EXCEPTION HANDLING IN PYTHON

Even if a statement or expression is syntactically correct, it may cause an error


when an attempt is made to execute it. Errors detected during execution are
called exceptions and are not unconditionally fatal

'2' + 2

try:

print(x)

except:

print("An exception occurred")

#The try block will generate an error, because x is not defined:

1 a=int(input('enter any value'))

2 try:

4 print(a/0,'is zero')

5 except:

6 print("ERRORRRRR")

ZeroDivisionError: division by zero


Handling Exceptions¶
It is possible to write programs that handle selected exceptions

Example
Print one message if the try block raises a NameError and another for other
errors:

try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")

In this example, the try block does not generate any error:

try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
Finally
The finally block, if specified, will be executed regardless if the try block
raises an error or not.

Example
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")

Try to open and write to a file that is not writable:

try:

f = open("[Link]")

try:

[Link]("Lorum Ipsum")

except:

print("Something went wrong when writing to the file")

finally:

[Link]()

except:

print("Something went wrong when opening the file")

Raise an exception
As a Python developer you can choose to throw an exception if a condition
occurs.

To throw (or raise) an exception, use the raise keyword.

Example
Raise an error and stop the program if x is lower than 0:
x = -1

if x < 0:
raise Exception("Sorry, no numbers below zero")

The raise keyword is used to raise an exception.

You can define what kind of error to raise, and the text to print to the user.

Example
Raise a TypeError if x is not an integer:

x = "hello"

if not type(x) is int:


raise TypeError("Only integers are allowed")

You might also like