0

I am looking at code in python that has a try: except Exception block. I have indentified the code that could potentially raise a ValueError.

My question: Does it make sense (or is a good practice) to include the ValueError in the except clause (in addition to the Exception which already encompasses ValueError)?

try:
 func_raises_value_error()
 func_raises_unknown_error()
except (ValueError, Exception) as e:
 pass
boardrider
6,2737 gold badges59 silver badges92 bronze badges
asked Apr 13, 2015 at 15:51
1
  • 1
    Please edit your post to include the code so that we can see what you're talking about. Commented Apr 13, 2015 at 15:54

2 Answers 2

5

It is absolutely good practice to catch specific errors. There are two general guidelines for use of try: except::

  1. Keep the try block as short as possible; and
  2. Be as specific as possible about which errors you want to handle.

So rather than e.g.

try:
 print("Please enter your name")
 name = input(" > ")
 print("Please enter your age")
 age = int(input(" > "))
 print("{} is {} years old".format(name, age))
except Exception:
 print("Something went wrong")

you should have:

print("Please enter your name")
name = input(" > ")
print("Please enter your age")
try:
 age = int(input(" > "))
except ValueError:
 print("That's not a number")
else:
 print("{} is {} years old".format(name, age))

Note that this has allowed a much more specific error message, and allowed any errors that weren't anticipated to pass up to the caller (per the Zen of Python: "Errors should never pass silently. Unless explicitly silenced.")


In your specific case, there is no point to using except (ValueError, Exception) as e:, for two reasons:

  1. Exception already incorporates ValueError; and
  2. You don't actually use e for anything.

If there is nothing you can (or want to) do about the errors raised by either function, you might as well just use except Exception: (which is better than a bare except:, at least).

answered Apr 13, 2015 at 15:56
Sign up to request clarification or add additional context in comments.

5 Comments

Notice I didn't actually get rid of the Exception. I realize that its good practice to be specifc as possible with exceptions. My idea here is when the unknown excpetions are actually known, exception can then be removed.
Thanks for you response , goes without saying!
@Pradyot why are the exceptions unknown? Doesn't the function documentation tell you what it might raise? If all you want to do is pass then it doesn't really matter, but could you not be doing something more useful?
True, the pass was just a short cut I took. The actual code just logs an error message.
@Pradyot well if you provide more context, I might be able to offer a more helpful answer! If you just want to log and move on, your current code is probably ok.
1

A good habit when dealing with multiple exceptions which can occur in a block of code is to process them from the most specific one to the most general one. As an example, your code could be written as:

try:
 func_raises_value_error()
 func_raises_unknown_error()
except ValueError as e:
 print 'Invalid value specified: %s' % e
except Exception as e:
 print 'A totally unexpected exception occurred: %s' % e
answered Apr 14, 2015 at 10:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.