Python - Try & Except
OverviewQuestions:Objectives:
How do I try to execute code, knowing it might fail?
What are some situations where this is important?
How can I write my own exceptions.
Requirements:
catch an exception
raise your own exception
Time estimation: 20 minutesLevel: Introductory IntroductorySupporting Materials:Last modification: Sep 28, 2022
Best viewed in a Jupyter NotebookThis tutorial is best viewed in a Jupyter notebook! You can load this notebook one of the following ways
Launching the notebook in Jupyter in Galaxy
- Instructions to Launch JupyterLab
- Open a Terminal in JupyterLab with File -> New -> Terminal
- Run
wget https://training.galaxyproject.org/training-material/topics/data-science/tutorials/python-exceptions/data-science-python-exceptions.ipynb
- Select the notebook that appears in the list of files on the left.
Downloading the notebook
- Right click one of these links: Jupyter Notebook (With Solutions), Jupyter Notebook (Without Solutions)
- Save Link As..
Try/except are a construct in Python used to catch a potential exception. Sometimes things go wrong in your code! Or in someone else’s code in a module. Sometimes some errors might be expected like when you try and read a user supplied file, maybe it isn’t available because they’ve specified the wrong path.
AgendaIn this tutorial, we will cover:
Raise
When you’re writing code, sometimes there are errors you might need to handle:
- If you’re calculating the mean of a set of numbers, and the list is empty. You don’t want to divide by
len(numbers)
and trigger a Zero Division Error. - If you’re opening a file, there maybe a chance that the file is not found.
Using try/excepts allow you to:
- provide friendlier and more useful error messages
- handle expected error cases
For instance, returning to the mean()
function example, what should mean([])
return for a hypothetical mean function that calculates the average of a list of numbers. Should it return 0? It probably should return an error. Let’s look at some example code:
def mean(numbers):
return sum(numbers) / len(numbers)
mean([1, 2, 3])
mean([])
This raises a ZeroDivisionError
but we can make this a more friendly error message by raising our own exception.
def mean(numbers):
if not numbers:
raise ValueError("You are calculating the mean of an empty list, which is not possible.")
return sum(numbers) / len(numbers)
mean([1, 2, 3])
mean([])
There are loads of different types of exception codes! The python documentation has a large list of exceptions and some descriptions for when or why those exceptions might be raised.
Now we get a much more useful error message from the function! Using raise
is especially important for library authors, people writing python modules that we all use. If they provide useful error messages, it helps you as an end user understand what’s happening.
Try & Except
Let’s look at how you would handle one of these exceptions, we’ll continue with the mean()
example above.
numbers = []
try:
result = mean(numbers)
print(f"Result is {result}")
except ValueError:
print("We cannot calculate the mean")
Here we use try:
to setup a new block, and this code is tried, Python attempts to execute it. Below are one or more except:
blocks which catch specific errors. Here we have specifically said we know a ValueError can happen, and decided to handle it.
Or for another example, accessing a user supplied file. Oftentimes users will call your program and supply a non-existent, or inacessible file. Here you can use multiple except
blocks to catch all of those potential errors.
user_supplied_file = 'does-not-exist.txt'
try:
open(user_supplied_file, 'r')
except FileNotFoundError:
print(f"The path you supplied ({user_supplied_file}) doesn't exist, please double check it!")
except PermissionError:
print(f"You supplied a valid file, but it is unreadable. Try changing it's permissions with `chmod +r {user_supplied_file}`")
Failing to open a file raises a FileNotFoundError
which indicates the file isn’t available, and PermissionError
indicates that a file is unreadable. However in practice, sometimes you’ll see something like this:
# Bad!
try:
doSomething()
except:
print("error")
This is called a bare exception, and will catch any exception, compared with except ValueError
which only catches value errors. People consider this generally a bad idea, termed code smell. (Because it smells (appears) bad!)
Finally
The last portion of the try:
/except:
block is finally:
, a third block which lets us do cleanup. It’s often very nice to your users that if your program fails halfway through, that you cleanup after yourself.
import os
try:
with open('gene_query.fa', 'w') as handle:
handle.write(">some fasta sequence we want to search against a database")
# runQuery('gene_query.fa')
# But here we have an error! Something goes wrong!
1 / 0
except:
# And now our results are invalid.
print("Something went wrong! Oh no! Check your inputs.")
finally:
# So we should cleanup this temporary file we created, so it doesn't cause
# problems or distract the user from the results file.
# This function will delete a file from your computer:
os.remove('gene_query.fa')
Fallback
Sometimes we can use try
/except
to have a fallback option. Consider the pseudocode below:
try:
runBlast()
except BlastNotAvailable:
try:
runBLAT()
except BLATnotAvailable:
print("Neither Blast nor BLAT were available.")
Sometimes you have a fallback option, some other tool you can use in its place. When that’s possible, you can use try
and except
to handle those cases and work around potential issues. But this isn’t always the case, sometimes you just need to print your error message and stop executing code.
Key points
raise lets your raise your own
Exception
sThis is mostly used by library authors (which you might become!)
Use
try
/except
to catch expected errors and work around them (if possible)finally lets you cleanup your temporary files, if you created some.
Frequently Asked Questions
Have questions about this tutorial? Check out the FAQ page for the Foundations of Data Science topic to see if your question is listed there. If not, please ask your question on the GTN Gitter Channel or the Galaxy Help ForumFeedback
Did you use this material as an instructor? Feel free to give us feedback on how it went.
Did you use this material as a learner or student? Click the form below to leave feedback.
Citing this Tutorial
- Helena Rasche, Donny Vrins, Bazante Sanders, 2022 Python - Try & Except (Galaxy Training Materials). https://training.galaxyproject.org/training-material/topics/data-science/tutorials/python-exceptions/tutorial.html Online; accessed TODAY
- Batut et al., 2018 Community-Driven Data Analysis Training for Biology Cell Systems 10.1016/j.cels.2018.05.012
Congratulations on successfully completing this tutorial!@misc{data-science-python-exceptions, author = "Helena Rasche and Donny Vrins and Bazante Sanders", title = "Python - Try & Except (Galaxy Training Materials)", year = "2022", month = "09", day = "28" url = "\url{https://training.galaxyproject.org/training-material/topics/data-science/tutorials/python-exceptions/tutorial.html}", note = "[Online; accessed TODAY]" } @article{Batut_2018, doi = {10.1016/j.cels.2018.05.012}, url = {https://doi.org/10.1016%2Fj.cels.2018.05.012}, year = 2018, month = {jun}, publisher = {Elsevier {BV}}, volume = {6}, number = {6}, pages = {752--758.e1}, author = {B{\'{e}}r{\'{e}}nice Batut and Saskia Hiltemann and Andrea Bagnacani and Dannon Baker and Vivek Bhardwaj and Clemens Blank and Anthony Bretaudeau and Loraine Brillet-Gu{\'{e}}guen and Martin {\v{C}}ech and John Chilton and Dave Clements and Olivia Doppelt-Azeroual and Anika Erxleben and Mallory Ann Freeberg and Simon Gladman and Youri Hoogstrate and Hans-Rudolf Hotz and Torsten Houwaart and Pratik Jagtap and Delphine Larivi{\`{e}}re and Gildas Le Corguill{\'{e}} and Thomas Manke and Fabien Mareuil and Fidel Ram{\'{\i}}rez and Devon Ryan and Florian Christoph Sigloch and Nicola Soranzo and Joachim Wolff and Pavankumar Videm and Markus Wolfien and Aisanjiang Wubuli and Dilmurat Yusuf and James Taylor and Rolf Backofen and Anton Nekrutenko and Björn Grüning}, title = {Community-Driven Data Analysis Training for Biology}, journal = {Cell Systems} }
Do you want to extend your knowledge? Follow one of our recommended follow-up trainings: