Friday 29 August 2014

No exception type(s) specified :: Pylint :: W0702

Error:

No exception type(s) specified

Description

Raised by Pylint, when an except clause doesn't specify exceptions to catch. This error is not fatal but considered a bad practice, good practice require to catch specific exception like ZeroDivisionError.

Example(s)

Given sample of code describe basic divide function, it takes two arguments a and b. We have simple divided first argument with second and returned result. Although their exist potential exception like ZeroDivisionError, we have used general exception to catch errors.

def divide(a, b):
    """
    function to take two arguments to divide first by second
    """
    try:
        result = a/b
    except:
        result = 0
    return result

Solution(s)

Good programming practice is to catch only a very specific range of types. Since every error in Python raises an exception, using except: can make many programming errors look like runtime problems, which hinders the debugging process. This may mask bugs in your code that would be quicker to diagnose if they weren't caught at all, or possibly would be better dealt with by a single very high level exception handler. Because except: catches all exceptions, including SystemExit, KeyboardInterrupt, and GeneratorExit (which is not an error and should not normally be caught by user code), using a bare except: is almost never a good idea. In situations where you need to catch all “normal” errors, such as in a framework that runs callbacks, you can catch the base class for all normal exceptions, Exception. Unfortunately in Python 2.x it is possible for third-party code to raise exceptions that do not inherit from Exception, so in Python 2.x there are some cases where you may have to use a bare except: and manually re-raise the exceptions you don’t want to catch.

Use  Specific Exceptions
Use more specific exceptions like  ZeroDivisionError, TypeError.
def divide(a, b):
    """
    function to take two arguments to divide first by second
    """
    try:
        result = a/b
    except ZeroDivisionError:
        result = 0
    except TypeError:
        result = 0
    return result