How to continue looping after a module defined exception in Python?


How to continue looping after a module defined exception in Python?



I loop though a list of currencies in order to download price series from an API and it happens that some of them are not supported so that it raises a module defined exception class : ExchangeError: This currency pair is not supported.


ExchangeError: This currency pair is not supported



When it occurs I would like to continue the loop to the next currency but for some reason I'm unable to handle the module exception.



Here is an example that works fine with a built-in exception :


f = [1,2,3,4,'A',5]

def foo(nb):
return nb /2

for i in f :

try:
print(foo(i))

except TypeError :
continue



As expected it returns :


0.5
1
1.5
2
2.5



But as soon as it is a module (or user defined) exception it throws an error saying the exception is not defined :


@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)
def apiFetchOHLC(obj, currency, timeframe, option):

ohlcv = obj().fetch_ohlcv(currency, timeframe, since = option)
return ohlc


for c in currencies_list :
...
try :
# Download data
ohlc = apiFetchOHLC(obj, c, tf, maxCandlesLimit)

# except : # works fine
except ExchangeError : # doesn't work
print("Oops! That was no valid currency. Continue...")
continue



This is the error I get when I run the loop :


except ExchangeError:
NameError: name 'ExchangeError' is not defined



To make it works I need to remove the exception type ExchangeError but to me it is not a workaround because it will continue the loop whatever the exception is, and sometimes I need to retry the download.


ExchangeError



How can I achieve this with try and except or with the retrying package ? (link)


try


except


retrying





Weird, you're code should work. Coul you provide some data so we can test it?
– Mathieu
Jun 29 at 12:11





To make it work I need to remove the exception type ExchangeError inside the loop like this except : instead of except ExchangeError :. Loop is running at the moment but I'll try to provide data asap.
– Florent
Jun 29 at 12:40



ExchangeError


except :


except ExchangeError :





@Mathieu I edited the question after I better identify the problem. It seems related with the module exception ExchangeError which is not defined inside the loop, whereas another example with a built-in exception works fine.
– Florent
Jun 29 at 13:03



ExchangeError





@Florent did you create custom exception to handle this? Can you please add where you defined ExchangeError ?
– Manoj Jadhav
Jun 29 at 13:06



ExchangeError





@ManojJadhav I use a package with pre-defined class exceptions, in the example it's called with obj().fetch_ohlcv()
– Florent
Jun 29 at 13:08


obj().fetch_ohlcv()




2 Answers
2


def foo(count):
try:
while(count < 10):
if(count%2 == 1):
raise Exception()
print(count)
count = count+1
except:
print( str(count) + ' -> Exception')
foo(count+1)

foo(2)



Whenever an exception occurs in a try block, handle it in except block as follows -


try


try


except


try


function


foo


except



After identifying the problem better I have found that I needed to give the full name space of the exception class I want to catch:


for c in currencies_list :
...
try :
# Download data
ohlc = apiFetchOHLC(obj, c, tf, maxCandlesLimit)

except ccxt.ExchangeError :
print("Oops! That was no valid currency. Continue...")
continue






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift