I am bit confused about the try exception usage in Python 2.7.
try:
raise valueError("sample value error")
except Exception as e:
print str(e)
try:
raise valueError("sample value error")
except Exception,exception:
print str(exception)
try:
raise valueError("sample value error")
except exception:
print str(exception)
try:
raise valueError("sample value error")
except Exception:
print str(Exception) # it prints only the object reference
can some help me to understand the above usage?
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions. In general, when a Python script encounters a situation that it cannot cope with, it raises an exception. An exception is a Python object that represents an error.
TryCatch and Try::Tiny both allow use of try/catch/finally syntax instead of boilerplate to handle exceptions correctly.
Python Try Except Else Example try: <--program code--> except <--Exception Type 1-->: <--exception handling code--> except <--Exception Type 2-->: <--exception handling code--> except <--Exception Type 3-->: <--exception handling code--> ... else: <--program code to run if "try" block doesn't encounter any error--> ...
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.
Some concepts to help you understand the difference between the alternate variants of the except
variants:
except Exception, e
– This in an older variant, now deprecated, similar to except Exception as e
except Exception as e
– Catch exceptions of the type Exception
(or any subclass) and store them in the variable e
for further processing, messaging or similarexcept Exception
– Catch exceptions of the type Exception
(or any subclass), but ignore the value/information provided in the exceptionexcept e
– Gives me an compilation error, not sure if this related to python version, but if so, it should/would mean that you don't care about the type of exception but want to access the information in itexcept
– Catch any exception, and ignore the exception informationWhat to use, depends on many factors, but if you don't need the provided information in the exception there is no need to present the variable to catch this information.
Regarding which type of Exception
to catch, take care to catch the accurate type of exceptions. If you are writing a general catch it all, it could be correct to use except Exception
, but in the example case you've given I would opt for actually using except ValueError
directly. This would allow for potentially other exceptions to be properly handled at another level of your code. The point is, don't catch exception you are not ready to handle.
If you want, you can read more on python 2.7 exception handling or available python 2.7 exception in the official documentation.
For Python 3 (also works in Python 2.7):
try:
raise ValueError("sample value error")
except Exception as e:
print(e)
For Python 2 (will not work in Python 3):
try:
raise ValueError("sample value error")
except Exception, e:
print e
I use:
try:
raise valueError("sample value error")
except Exception as e:
print str(e)
When I want to declare a specific error and
try:
raise valueError("sample value error")
except:
print "Something unexpected happened"
When I don't really care or except: pass
, except: return
etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With