This question already has answers here:
Python try...except comma vs 'as' in except
(5 answers)
Closed 9 years ago.
I am just being curious about the syntax of python exceptions as I can't seem to understand when you are suppossed to use the syntax below to catch an exception.
try:
"""
Code that can raise an exception...
"""
except Exception as e:
pass
and
try:
"""
Code that can raise an exception...
"""
except Exception, e:
pass
What is the difference?
Note: As Martijn points out, comma variable form is deprecated in Python 3.x. So, its always better to use as form.
As per http://docs.python.org/2/tutorial/errors.html#handling-exceptions
except Exception, e:
is equivalent to
except Exception as e:
Commas are still used when you are catching multiple exceptions at once, like this
except (NameError, ValueError) as e:
Remember, the parentheses around the exceptions are mandatory when catching multiple exceptions.
except Exception, e is deprecated in Python 3.
The proper form is:
try:
...
except Exception as e:
...
See: http://docs.python.org/3.0/whatsnew/2.6.html#pep-3110
Related
This question already has answers here:
What does ''except Exception as e'' mean in python? [closed]
(1 answer)
Difference between except: and except Exception as e:
(5 answers)
Closed 3 months ago.
try:
0/0
except Exception as e:
print(e)
The above code prints division by zero as one would expect. But if we try to print without creating the alias:
try:
0/0
except Exception:
print(Exception)
It simply prints <class 'Exception'>. What is happening here? The as keyword is used to create an "alias". If the error message "division by zero" is an attribute of the Exception class, then why does creating an alias make it equal to said attribute?
Is it possible to print the error message without creating the alias?
The documentation of Python states this:
The except clause may specify a variable after the exception name. The variable is bound to the exception instance which typically has an args attribute that stores the arguments. For convenience, builtin exception types define str() to print all the arguments without explicitly accessing .args.
It means that the variable that is passed after the name of Exception has additional attributes to display the specific exception occured.
If I have that code:
try:
some_method()
except Exception, e:
How can I get this Exception value (string
representation I mean)?
use str
try:
some_method()
except Exception as e:
s = str(e)
Also, most exception classes will have an args attribute. Often, args[0] will be an error message.
It should be noted that just using str will return an empty string if there's no error message whereas using repr as pyfunc recommends will at least display the class of the exception. My take is that if you're printing it out, it's for an end user that doesn't care what the class is and just wants an error message.
It really depends on the class of exception that you are dealing with and how it is instantiated. Did you have something in particular in mind?
Use repr() and The difference between using repr and str
Using repr:
>>> try:
... print(x)
... except Exception as e:
... print(repr(e))
...
NameError("name 'x' is not defined")
Using str:
>>> try:
... print(x)
... except Exception as e:
... print(str(e))
...
name 'x' is not defined
Even though I realise this is an old question, I'd like to suggest using the traceback module to handle output of the exceptions.
Use traceback.print_exc() to print the current exception to standard error, just like it would be printed if it remained uncaught, or traceback.format_exc() to get the same output as a string. You can pass various arguments to either of those functions if you want to limit the output, or redirect the printing to a file-like object.
Another way hasn't been given yet:
try:
1/0
except Exception, e:
print e.message
Output:
integer division or modulo by zero
args[0] might actually not be a message.
str(e) might return the string with surrounding quotes and possibly with the leading u if unicode:
'integer division or modulo by zero'
repr(e) gives the full exception representation which is not probably what you want:
"ZeroDivisionError('integer division or modulo by zero',)"
edit
My bad !!! It seems that BaseException.message has been deprecated from 2.6, finally, it definitely seems that there is still not a standardized way to display exception messages. So I guess the best is to do deal with e.args and str(e) depending on your needs (and possibly e.message if the lib you are using is relying on that mechanism).
For instance, with pygraphviz, e.message is the only way to display correctly the exception, using str(e) will surround the message with u''.
But with MySQLdb, the proper way to retrieve the message is e.args[1]: e.message is empty, and str(e) will display '(ERR_CODE, "ERR_MSG")'
To inspect the error message and do something with it (with Python 3)...
try:
some_method()
except Exception as e:
if {value} in e.args:
{do something}
The following worked for me:
import traceback
try:
some_method()
except Exception as e:
# Python 3.9 or older
print("".join(traceback.format_exception_only(type(e), e)).strip())
# Python 3.10+
print("".join(traceback.format_exception_only(e)).strip())
If some_method() raises the exception ValueError("asdf"), this prints what you see in tracebacks--minus the traceback: ValueError: asdf.
Here is the documentation on this.
For python2, It's better to use e.message to get the exception message, this will avoid possible UnicodeDecodeError. But yes e.message will be empty for some kind of exceptions like OSError, in which case we can add a exc_info=True to our logging function to not miss the error.
For python3, I think it's safe to use str(e).
I know that I can do:
try:
# do something that may fail
except:
# do this if ANYTHING goes wrong
I can also do this:
try:
# do something that may fail
except IDontLikeYouException:
# say please
except YouAreTooShortException:
# stand on a ladder
But if I want to do the same thing inside two different exceptions, the best I can think of right now is to do this:
try:
# do something that may fail
except IDontLikeYouException:
# say please
except YouAreBeingMeanException:
# say please
Is there any way that I can do something like this (since the action to take in both exceptions is to say please):
try:
# do something that may fail
except IDontLikeYouException, YouAreBeingMeanException:
# say please
Now this really won't work, as it matches the syntax for:
try:
# do something that may fail
except Exception, e:
# say please
So, my effort to catch the two distinct exceptions doesn't exactly come through.
Is there a way to do this?
From Python Documentation:
An except clause may name multiple exceptions as a parenthesized tuple, for example
except (IDontLikeYouException, YouAreBeingMeanException) as e:
pass
Or, for Python 2 only:
except (IDontLikeYouException, YouAreBeingMeanException), e:
pass
Separating the exception from the variable with a comma will still work in Python 2.6 and 2.7, but is now deprecated and does not work in Python 3; now you should be using as.
How do I catch multiple exceptions in one line (except block)
Do this:
try:
may_raise_specific_errors():
except (SpecificErrorOne, SpecificErrorTwo) as error:
handle(error) # might log or have some other default behavior...
The parentheses are required due to older syntax that used the commas to assign the error object to a name. The as keyword is used for the assignment. You can use any name for the error object, I prefer error personally.
Best Practice
To do this in a manner currently and forward compatible with Python, you need to separate the Exceptions with commas and wrap them with parentheses to differentiate from earlier syntax that assigned the exception instance to a variable name by following the Exception type to be caught with a comma.
Here's an example of simple usage:
import sys
try:
mainstuff()
except (KeyboardInterrupt, EOFError): # the parens are necessary
sys.exit(0)
I'm specifying only these exceptions to avoid hiding bugs, which if I encounter I expect the full stack trace from.
This is documented here: https://docs.python.org/tutorial/errors.html
You can assign the exception to a variable, (e is common, but you might prefer a more verbose variable if you have long exception handling or your IDE only highlights selections larger than that, as mine does.) The instance has an args attribute. Here is an example:
import sys
try:
mainstuff()
except (KeyboardInterrupt, EOFError) as err:
print(err)
print(err.args)
sys.exit(0)
Note that in Python 3, the err object falls out of scope when the except block is concluded.
Deprecated
You may see code that assigns the error with a comma. This usage, the only form available in Python 2.5 and earlier, is deprecated, and if you wish your code to be forward compatible in Python 3, you should update the syntax to use the new form:
import sys
try:
mainstuff()
except (KeyboardInterrupt, EOFError), err: # don't do this in Python 2.6+
print err
print err.args
sys.exit(0)
If you see the comma name assignment in your codebase, and you're using Python 2.5 or higher, switch to the new way of doing it so your code remains compatible when you upgrade.
The suppress context manager
The accepted answer is really 4 lines of code, minimum:
try:
do_something()
except (IDontLikeYouException, YouAreBeingMeanException) as e:
pass
The try, except, pass lines can be handled in a single line with the suppress context manager, available in Python 3.4:
from contextlib import suppress
with suppress(IDontLikeYouException, YouAreBeingMeanException):
do_something()
So when you want to pass on certain exceptions, use suppress.
From Python documentation -> 8.3 Handling Exceptions:
A try statement may have more than one except clause, to specify
handlers for different exceptions. At most one handler will be
executed. Handlers only handle exceptions that occur in the
corresponding try clause, not in other handlers of the same try
statement. An except clause may name multiple exceptions as a
parenthesized tuple, for example:
except (RuntimeError, TypeError, NameError):
pass
Note that the parentheses around this tuple are required, because
except ValueError, e: was the syntax used for what is normally
written as except ValueError as e: in modern Python (described
below). The old syntax is still supported for backwards compatibility.
This means except RuntimeError, TypeError is not equivalent to
except (RuntimeError, TypeError): but to except RuntimeError as
TypeError: which is not what you want.
If you frequently use a large number of exceptions, you can pre-define a tuple, so you don't have to re-type them many times.
#This example code is a technique I use in a library that connects with websites to gather data
ConnectErrs = (URLError, SSLError, SocketTimeoutError, BadStatusLine, ConnectionResetError)
def connect(url, data):
#do connection and return some data
return(received_data)
def some_function(var_a, var_b, ...):
try: o = connect(url, data)
except ConnectErrs as e:
#do the recovery stuff
blah #do normal stuff you would do if no exception occurred
NOTES:
If you, also, need to catch other exceptions than those in the
pre-defined tuple, you will need to define another except block.
If you just cannot tolerate a global variable, define it in main()
and pass it around where needed...
One of the way to do this is..
try:
You do your operations here;
......................
except(Exception1[, Exception2[,...ExceptionN]]]):
If there is any exception from the given exception list,
then execute this block.
......................
else:
If there is no exception then execute this block.
and another way is to create method which performs task executed by except block and call it through all of the except block that you write..
try:
You do your operations here;
......................
except Exception1:
functionname(parameterList)
except Exception2:
functionname(parameterList)
except Exception3:
functionname(parameterList)
else:
If there is no exception then execute this block.
def functionname( parameters ):
//your task..
return [expression]
I know that second one is not the best way to do this, but i'm just showing number of ways to do this thing.
As of Python 3.11 you can take advantage of the except* clause that is used to handle multiple exceptions.
PEP-654 introduced a new standard exception type called ExceptionGroup that corresponds to a group of exceptions that are being propagated together. The ExceptionGroup can be handled using a new except* syntax. The * symbol indicates that multiple exceptions can be handled by each except* clause.
For example, you can handle multiple exceptions
try:
raise ExceptionGroup('Example ExceptionGroup', (
TypeError('Example TypeError'),
ValueError('Example ValueError'),
KeyError('Example KeyError'),
AttributeError('Example AttributeError')
))
except* TypeError:
...
except* ValueError as e:
...
except* (KeyError, AttributeError) as e:
...
For more details see PEP-654.
This question already has answers here:
Manually raising (throwing) an exception in Python
(11 answers)
Closed 4 years ago.
I have read the official definition of "raise", but I still don't quite understand what it does.
In simplest terms, what is "raise"?
Example usage would help.
It has two purposes.
jackcogdill has given the first one:
It's used for raising your own errors.
if something:
raise Exception('My error!')
The second is to reraise the current exception in an exception handler, so that it can be handled further up the call stack.
try:
generate_exception()
except SomeException as e:
if not can_handle(e):
raise
handle_exception(e)
raise without any arguments is a special use of python syntax. It means get the exception and re-raise it. If this usage it could have been called reraise.
raise
From The Python Language Reference:
If no expressions are present, raise re-raises the last exception that
was active in the current scope.
If raise is used alone without any argument is strictly used for reraise-ing. If done in the situation that is not at a reraise of another exception, the following error is shown:
RuntimeError: No active exception to reraise
It's used for raising errors.
if something:
raise Exception('My error!')
Some examples here
Besides raise Exception("message") and raise Python 3 introduced a new form, raise Exception("message") from e. It's called exception chaining, it allows you to preserve the original exception (the root cause) with its traceback.
It's very similar to inner exceptions from C#.
More info:
https://www.python.org/dev/peps/pep-3134/
You can use it to raise errors as part of error-checking:
if (a < b):
raise ValueError()
Or handle some errors, and then pass them on as part of error-handling:
try:
f = open('file.txt', 'r')
except IOError:
# do some processing here
# and then pass the error on
raise
raise causes an exception to be raised. Some other languages use the verb 'throw' instead.
It's intended to signal an error situation; it flags that the situation is exceptional to the normal flow.
Raised exceptions can be caught again by code 'upstream' (a surrounding block, or a function earlier on the stack) to handle it, using a try, except combination.
Then using try catch in python you can catch errors and assign them to a variable with the as keyword
try
do something..
except IOError as e:
do something with e..
However then trying to do the same thing without knowing the type of error python complains about the syntax.
try
do something..
except as e:
do something with e..
Is there any way to catch a default error and assign it to a variable?
Yes there is. All exceptions derive from the Exception class.
So you can do:
try:
doSomething()
except Exception as e:
doSomethingWithException(e)
It is a kind of catch-all line.
You can also use sys.exc_info(). This allows you to handle exceptions on Python 2.x and Python 3.x with the same code.
The conventional manner is:
try:
do_whatever()
except Exception as e:
handle_it()
Although it's ill-advised to catch such broad excepts - it's preferable to catch specific exceptions you know you can handle and let anything else propogate.
It's worth noting that KeyboardInterrupt and SystemExit inherit from BaseException and not Exception, so these wouldn't be caught were you expecting to cater for those, but that shouldn't be a problem as they should be handled at the top level anyway.