-2

I am trying this code:

try:
    res = subprocess.Popen('bgpq3 -4 {} -m 24 -l {}'.format('MAIyNT- 
AS38082','12414'), shell=True,
    universal_newlines=True,
    stdout=subprocess.PIPE).communicate()[0]
except Exception:
       print("Wrong")
       #do this code 

The output is ?

ERROR:Unable to parse prefix 'MAIyNT-AS38082', af=2 (inet), ret=0
ERROR:Unable to parse prefix MAIyNT-AS38082
ERROR:Unable to add prefix MAIyNT-AS38082 (bad prefix or address-family)

so i am not able to use Error handling!!

Any Idea?

ete eteq
  • 17
  • 4

2 Answers2

0

You're only handling errors of type exception here. You only need to use except:. This way you're catching all errors that occur in the code.

try:
    #your code
except Exception as e:
    #handle the exception

For more information refer to the docs I got from a quick google ;)

Jab
  • 26,853
  • 21
  • 75
  • 114
  • Thanks for the knowledge. @Gsk I was only suggesting as a quick dirty fix. I edited my answer. Although yours is more useful. – Jab Oct 04 '18 at 12:34
0

when you write except Exception: you are not catching all the exception: the system exit errors and OS errors (as BaseException, SystemExit, KeyboardInterrupt and GeneratorExit) are excluded.

Most of the exceptions of subprocess are OSError.
Since you did not report the full traceback of the error, I may only assume that you're getting one of these errors, and you can catch them using:

except subprocess.CalledProcessError:

or

except OSError:

as PEP 8 suggest, you should NOT use except: alone, even if it will work in your case.
As a rule of thumb, always catch the exact exception you espect to be raised!

Gsk
  • 2,929
  • 5
  • 22
  • 29