handle two different erro codes by exception handler in python
Question: HI i am new python and please excuse me if this seems to be a silly question .
I have a function in my code which returns an exception ResponseError and the
ResponseError has two error codes 404 and 403 I want my exception handler to
give two different messages based on the error code if the error code is 404
it should say "file not exist " if the error code is 403 then "forbidden to
access".


Answer: If you are using urllib2 package in python then you could handle like this:

    
    
    import urllib2
    try:
       urllib2.urlopen("any url")
    except urllib2.HTTPError, err:
       if err.code == 404:
           print "Files does not exit"
       elif err.code == 403:
           print "Forbidden to Access"
    

For example the below code will print out "files does not exist"

    
    
    import urllib2
    try:
       urllib2.urlopen("http://www.google.com/events")
    except urllib2.HTTPError, err:
       if err.code == 404:
           print "Files does not exit"
       elif err.code == 403:
           print "Forbidden to Access"
    
     output - `Files does not exist` since the above url does not exist.  
    

