0

Is there a way to determine all possible exceptions that might be raised in a code block? (e.g. Some logic that suggests some specific exceptions to catch based on the code block instead of just raising that exception is too generic.

    try:
        m = check_output(["dd", "--version"]).decode()
        ver_line = m.split('\n')[0]
        ver = ver_line.split(' ')
        if float(ver[2]) >= 8.24:
            logger.info("coreutils version: {}, required >= 8.24".format(ver[2]))
        else:
            logger.warn("coreutils version: {}, required >= 8.24."
                        "Please ensure that the right version is installed".format(ver[2]))
            sys.exit(1)

    except Exception:
        logger.warn("could not determine coreutils version, required >= 8.24")
        return
Stefan Papp
  • 2,199
  • 1
  • 28
  • 54
  • You could print the message of that exception by replace "except Exception" with "except Exception as error" and than print the error variable, if this helps you somehow. – Silviu Jun 29 '18 at 16:58
  • In my experience, it's usually the case that I only want to catch exceptions from known causes (which I know are "okay"). If I wanted to catch all possible exceptions, your code does exactly that. Are you looking for something that will go through and identify possible types of errors and their causes? – Jay Calamari Jun 29 '18 at 17:02

1 Answers1

-1

with putting the Exceptions as e you can print the exception whatever it is You determinate the error by using

type(e) == IndexError

and then you compare it's values

a = ['a', 'b', 1, 3, 4, 5, 6]

for i in range(0, 20):
    try:
        print(int(a[i]))
    except Exception as e:
        if type(e) == IndexError:
            pass
        else:
            print("ERROR",e)
ThunderHorn
  • 1,975
  • 1
  • 20
  • 42