Jump to content
Sign in to follow this  
  • entries
    73
  • comments
    131
  • views
    55046

Python #10 - Exceptions

Sign in to follow this  
okonomiyaki

139 views

Exceptions in Python are handled using the standard "try/catch" statement, except Python uses "except" instead of "catch". Of course, it conforms to the syntax of Python. You can have several catch clauses with different types of exceptions. Something which I haven't seen before is the ability to add an "else" statement to the try/except block. Code in this block must executed after the "try" block if it does not raise an exception.

But why not just add code after the try/except block? That would do the same thing.

try:
x = (1,2,3)
except:
print 'Error!'
else:
(a,b,c) = x
print a, b, c


Ok, that works... but wouldn't this do that same thing?


try:
x = (1,2,3)
except:
print 'Error!'

(a,b,c) = x
print a, b, c


I don't see a point in the "else" statement.

Otherwise, here's an example of catching certain types of exceptions:

try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except IOError, (errno, strerror):
print "I/O error(%s): %s" % (errno, strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error!


--
Edit:

My question was answered. My mind is foggy today! Of course, the else block only executes when an exception doesn't occur. If an exception occurs, you can handle it and continue executing normally, but the else block is skipped.

I was thinking of the case where you threw the exception up, which would exit the function and there wouldn't be any use for 'else.'
Sign in to follow this  


2 Comments


Recommended Comments

Ok, here's the simple answer:
In your example, the else block is executed only if an exception is never thrown. On the other hand, if you drop the else, then the code is always executed, regardless of an exception or not.

Share this comment


Link to comment
Oh, wait...

You're right. I was assuming that whenever an exception occurred it just threw up back up and exited the function. I forgot about the case where you handle the exception and continue executing the function normally.
My example would be:

try:
x = (1,2,3)
except:
print 'Error!'
raise
else:
(a,b,c) = x
print a, b, c


In that scenario, the else doesn't matter.

Thanks for clearing that up.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!