python - Replacing if-else statement with Exception Handling -


i have function below has if-else statement within it. after reading this post thought may better use try-catch exception handling instead. however, not sure how this. basically, if currency inputted not aud want throw exception print statement below.

def update(self, currency):     if self.currency == 'aud':         url = 'http://www.rba.gov.au/statistics/tables/csv/f17-yields.csv'          response = urllib2.urlopen(url)         text = response.read()          csvfile = stringio.stringio(text)         df = pd.read_csv(csvfile)         print df      else:         print('this currency not available in database') 

you don't want raising , catching exception @ same place. instead, want raise exception error first noticed, , catch wherever makes sense report issue.

in code you've shown, want replace print call raise statement, of valueerror. pass text you're printing argument exception:

raise valueerror('this currency not available in database') 

since haven't shown update called, don't know sure appropriate catch exception. 1 reason exceptions useful (rather if/else tests) can let exception bubble out of several functions or structural blocks if there's no useful way handle them @ level.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -