Mehan CO
asked on
pass error on python3
i got this error while running my script
requests.exceptions.ConnectionError
, now i want create except for it , is this way is true ? except (BaseException, Exception, AttributeError, requests.exceptions.ConnectionError, ConnectionError) as err:
pass
ASKER
then my job is Okey ?
except (BaseException, Exception, AttributeError, requests.exceptions.ConnectionError, ConnectionError) as err:
As I said the line should be working to catch your exception (except the exception occurred in a thread or sub process)
But your line is way too complicated:
if you write
you have probably the same result:
just add in your code something like:
(False, <type 'exceptions.BaseException' >)
(True, <type 'exceptions.Exception'>)
(True, <type 'exceptions.AttributeError '>)
(True, <class 'requests.exceptions.Conne ctionError '>)
(True, <class 'requests.exceptions.Conne ctionError '>)
this means, that AttributeError requests.exceptions.Connec tionError are subclasses of Exception.
so if you catch Exception you do not have to explicitly cacth AttributeError requests.exceptions.Connec tionError
if you want to catch all exceptions (though this is depending on the context not necesasiry good practice , then better to just write
If you want to catch all but one exception then:
But your line is way too complicated:
if you write
except Exception, BaseException:
pass
you have probably the same result:
just add in your code something like:
excs = (BaseException, Exception, AttributeError, requests.exceptions.ConnectionError, ConnectionError)
for exc in excs:
print(isinstance(exc(""), Exception), exc)
you will probably get a result similiar to:(False, <type 'exceptions.BaseException'
(True, <type 'exceptions.Exception'>)
(True, <type 'exceptions.AttributeError
(True, <class 'requests.exceptions.Conne
(True, <class 'requests.exceptions.Conne
this means, that AttributeError requests.exceptions.Connec
so if you catch Exception you do not have to explicitly cacth AttributeError requests.exceptions.Connec
if you want to catch all exceptions (though this is depending on the context not necesasiry good practice , then better to just write
except:
pass
If you want to catch all but one exception then:
try:
your_code_with_exception
except ExceptionYouDontWantToCatch:
raise
except: # catch all other exceptions
print("rest caught here"
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Open in new window
(you have of course to import requests to make this work)
Of course I don't know the context of your error / your issues, but in many cases it makes sense to add a log statement / a print statement or
to add an error counter or a list of failed urls in the except statement.
Otherwise debugging will be very tricky if you just silent this exception.