0

I currently have the function below which has an if-else statement within it. After reading this post I thought it may be better to use try-catch Exception Handling instead. However, I am not sure how to do this. Basically, if the currency inputted is not AUD I want to throw an Exception with the 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 is not available in Database')
Community
  • 1
  • 1
Jojo
  • 1,117
  • 2
  • 15
  • 28
  • 3
    I think I am missing a part of what you're looking for. If you want to throw an exception when currency isn't `'AUD'`, just do so after checking for currency: `if self.currency != 'AUD': raise Exception` – a p Jun 18 '15 at 20:19
  • @ap I was hoping to raise the Exception along with the `print` statement above. – Jojo Jun 18 '15 at 20:22
  • `raise ValueError("...")` and then do the handling where you call `update` – tynn Jun 18 '15 at 20:24
  • I agree with the first comment – cecilphillip Jun 18 '15 at 20:25
  • @ap I didn't realize it would be appropriate to `raise` an Exception without having `try-catch` – Jojo Jun 18 '15 at 20:57
  • @cecilphillip So, the only change I would need to make to my code is replace the `else:` with `if self.currency != 'CAD': raise ValueError` and then follow this by the `print` statement. – Jojo Jun 18 '15 at 20:59

3 Answers3

2

You generally don't want to be raising and catching an exception at the same place. Instead, you want to raise the exception where the error is first noticed, and catch it wherever it makes sense to report the issue.

In the code you've shown, you just want to replace the print call with a raise statement, probably of a ValueError. Pass the text you're printing as an argument to the exception:

raise ValueError('This currency is not available in Database')

Since you haven't shown where update is called, I don't know for sure where it would be appropriate to catch the exception. One reason that exceptions are useful (rather than if/else tests) is that you can let the exception bubble out of several functions or structural blocks if there's no useful way to handle them at that level.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
0

Using exception handling instead of an if-else statement will be much slower.

I've benchmarked a similar comparison for locating a list of keys in a dictionary here, and timings are attached. For me, it was 5 times slower.

Community
  • 1
  • 1
StackG
  • 2,730
  • 5
  • 29
  • 45
-1

If you want to force exception handling, you can use assert:

def update(self, currency):
    try:
        assert 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

    except AssertionError:
        print('This currency is not available in Database')

Not necessarily ideal in this case (this is a LBYL scenario, in my opinion), since the equality test should be faster, more readable, and scale better to more currencies, assuming you start off with a wide variety of different currencies.

Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67