Difference between exception: and exception Exception like e: in Python

Both of the following code snippets do the same. They receive each exception and execute code in the except: block except:

Fragment 1 -

 try: #some code that may throw an exception except: #exception handling code 

Fragment 2 -

 try: #some code that may throw an exception except Exception as e: #exception handling code 

What is the difference in both designs?

+55
python
Sep 24 '13 at 13:11
source share
5 answers

In the second, you can access the attributes of the exception object:

 >>> def catch(): ... try: ... asd() ... except Exception as e: ... print e.message, e.args ... >>> catch() global name 'asd' is not defined ("global name 'asd' is not defined",) 

But it does not catch BaseException or SystemExit , KeyboardInterrupt and GeneratorExit exceptions:

 >>> def catch(): ... try: ... raise BaseException() ... except Exception as e: ... print e.message, e.args ... >>> catch() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in catch BaseException 

Which is naked except:

 >>> def catch(): ... try: ... raise BaseException() ... except: ... pass ... >>> catch() >>> 

See Built-in Exceptions in Documents and Errors and Exceptions in the tutorial for more information.

+64
Sep 24 '13 at 13:16
source share
 except: 

accepts all exceptions whereas

 except Exception as e: 

only accepts exceptions that you must catch.

Here is an example of what you are not going to catch:

 >>> try: ... input() ... except: ... pass ... >>> try: ... input() ... except Exception as e: ... pass ... Traceback (most recent call last): File "<stdin>", line 2, in <module> KeyboardInterrupt 

The first disabled KeyboardInterrupt !

Here is a quick list:

 issubclass(BaseException, BaseException) #>>> True issubclass(BaseException, Exception) #>>> False issubclass(KeyboardInterrupt, BaseException) #>>> True issubclass(KeyboardInterrupt, Exception) #>>> False issubclass(SystemExit, BaseException) #>>> True issubclass(SystemExit, Exception) #>>> False 

If you want to catch any of them, the best thing to do is

 except BaseException: 

to indicate that you know what you are doing.




All exceptions are related to BaseException , and those that you are going to catch daily (those that will be selected for the programmer) are also inherited from Exception .

+26
Sep 24 '13 at 13:17
source share

There are differences with some exceptions, for example. KeyboardInterrupt

Reading PEP8 :

Naked, except: the sentence will catch SystemExit and KeyboardInterrupt exceptions, making it difficult to interrupt a program using Control-C, and may mask other problems. If you want to catch all exceptions that signal program errors, use the exception Exception: (naked except equivalent to a BaseException :) exception.

+8
Sep 24 '13 at 13:18
source share

Using the second form gives you a variable (named according to the as clause in your example e ) in the scope of the except block with the exception object attached to it so that you can use the information in the exception (type, message, stack trace, etc.), to handle the exception in a specially equipped manor.

+2
Sep 24 '13 at 13:18
source share

an exception means an error. to detect runtime errors. Runtime error handling is called exception handling. It has two blocks to try. Besides trying: this .it block only accepts an exception. Except: this is a block. it accepts the exception and handles the exception. and done.

-3
Jun 28 '16 at 5:34
source share



All Articles