Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python Exception Handling with Try, Except, Else, and Finally, Cheat Sheet of Mathematics

Python's try, except, else, and finally blocks enable developers to test code for errors, handle exceptions, execute code when no error occurs, and release external resources, respectively. Examples of using these blocks to handle nameerrors and other exceptions.

Typology: Cheat Sheet

2022/2023

Uploaded on 12/22/2022

Sahil1239
Sahil1239 🇮🇳

4 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PythonTry Except
Thetryblock lets you test a block of code for errors.
Theexceptblock lets you handle the error.
Theelseblock lets you execute code when there is no error.
Thefinallyblock lets you execute code, regardless of the result of the try- and except blocks.
Exception Handling: When an error occurs, or exception as we call it, Python will normally stop
and generate an error message.These exceptions can be handled using thetrystatement:
Example
Thetryblock will generate an exception, becausexis not defined:
try:
print(x)
except:
print("An exception occurred")
Since the try block raises an error, the except block will be executed.
Without the try block, the program will crash and raise an error:
Example
This statement will raise an error, becausexis not defined:
print(x)
Many Exceptions
You can define as many exception blocks as you want, e.g. if you want to execute a special block of code for a special kind of
error:
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")
Python try...finally
Thetrystatement in Python can have an optionalfinallyclause. This clause is executed no matter what, and is
generally used to release external resources.
try:
print(x)
exceptNameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
finally:
print("Program did not run")

Partial preview of the text

Download Python Exception Handling with Try, Except, Else, and Finally and more Cheat Sheet Mathematics in PDF only on Docsity!

Python Try Except

The try block lets you test a block of code for errors.

The except block lets you handle the error.

The else block lets you execute code when there is no error.

The finally block lets you execute code, regardless of the result of the try- and except blocks.

Exception Handling: When an error occurs, or exception as we call it, Python will normally stop

and generate an error message.These exceptions can be handled using the try statement:

Example

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

try: print(x) except: print("An exception occurred") Since the try block raises an error, the except block will be executed. Without the try block, the program will crash and raise an error:

Example

This statement will raise an error, because x is not defined:

print(x)

Many Exceptions

You can define as many exception blocks as you want, e.g. if you want to execute a special block of code for a special kind of error:

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")

Python try...finally

The try statement in Python can have an optional finally clause. This clause is executed no matter what, and is generally used to release external resources. try: print(x) except NameError: print("Variable x is not defined") except: print("Something else went wrong") finally: print("Program did not run")