Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python except None

Tags:

python

Are there any unexpected side effects of using "except None"? The behavior I expect is that nothing will be caught by that clause which a few small tests seem to confirm.

Here is a rough outline of what I am trying to do. When no argument is provided to the function, exceptions=None which creates the "except None" clause. Just want to double check that I'm not going to catch something unexpected.

# exceptions is exception or set of exceptions I want to do special processing for
def check_exceptions(exceptions=None)
  try:                                                                            
    ...                                                                    
  except exceptions as e:                                                       
    ...                                                                 
like image 290
Ben J Avatar asked Oct 11 '13 21:10

Ben J


People also ask

How do I ignore a specific exception in Python?

Use pass to ignore an exception Within a try and except block, use pass in the except clause to indicate no action is required in handling the caught exception.

How do I get exception details in Python?

Catching Exceptions in Python In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.

Is there null pointer exception in Python?

There is no "invalid argument" or "null pointer" built-in exception in Python. Instead, most functions raise TypeError (invalid type such as NoneType ) or ValueError (correct type, but the value is outside of the accepted domain).

What is except in Python?

The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.


Video Answer


2 Answers

Works fine here (under Python 2.x).

>>> try:
...   foo
... except None as e:
...   pass
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'foo' is not defined

For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object, or a tuple containing an item compatible with the exception.

source

Therefore the expression doesn't have to be an exception type, it will simply fail to ever match.

This behavior was changed in Python 3.x, and the expression after except must be a descendant of BaseException or a tuple of such.

like image 159
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 07:09

Ignacio Vazquez-Abrams


Python 3

def check_exceptions(exceptions=())
  try:                                                                            
    ...                                                                    
  except exceptions as e:                                                       
    ...                              
like image 41
Ion de la Braila Avatar answered Sep 20 '22 07:09

Ion de la Braila