-1

I am trying to work out the most pythonic way of handling exceptions. Let's say we have a long method that completes multiple tasks to do with registering a car

e.g.

  1. Retrieving car details
  2. Calculating the tax
  3. Sending out a confirmation email

Should the calling method deal with exceptions:

 def process_car_registration(self, registration):
    try:
        car_details = self.retrieve_car_details(registration)
    except:
        car_details = None
        print("Cannot retrieve car details")

    try:
        car_tax = self.calculate_tax_due(registration)
    except:
        car_tax = None
        print("Cannot calculate tax due")

    try:
        self.send_confirmation_email(registration, car_details, car_tax)
    except:
        print("Cannot send confirmation email")


def calculate_tax_due(self, registration):
    return self.dal.get_car_tax(registration)

Or should the individual methods itself deal with exceptions. If there are any exceptions in this methods we just want to log it (or in this case print it) and continue.

 def process_car_registration(self, registration):

        car_details = self.retrieve_car_details(registration)

        car_tax = self.calculate_tax_due(registration)

        self.send_confirmation_email(registration, car_details, car_tax)


def calculate_tax_due(self, registration):
    try:
        return self.dal.get_car_tax(registration)
    except:
        print("Cannot calculate tax due")
        return None

Is one method more pythonic then the other or does it come down to which is more readable? I prefer the second way but we seem to use the first way more often.

user7692855
  • 1,582
  • 5
  • 19
  • 39
  • 3
    1. Why do you want to continue after an exception? How can you calculate tax due if you failed to retrieve car details? 2. Doing this will prevent you from seeing what the exception was and where it happened. – Alex Hall Aug 12 '18 at 12:19
  • This was just an generic example to see how other people handle with exceptions. But let's just say you can get tax from the registration and without the car details. So the email sent at the end will just send out tax details and not car details. – user7692855 Aug 12 '18 at 12:25
  • Cross-language dupe: [When to catch the Exception vs When to throw the Exceptions?](//stackoverflow.com/q/18679090) – Aran-Fey Aug 12 '18 at 12:32
  • 1
    Where you handle an exception is entirely dependent on the application. There is no one-size-fits-all approach. You handle them where your application requirements say it makes sense they need to be handled. – Martijn Pieters Aug 12 '18 at 12:32
  • This is why Alex is pointing out where *in this example* you should handle the exceptions. If your application requirements state that if car details are not available that you just continue without, then *that's an application requirement*, and you handle the exception there. This applies to any other programming logic too, not just exceptions. – Martijn Pieters Aug 12 '18 at 12:35
  • Put differently, you are asking us to tell you where to use a `while` loop or when to commit changes to the database. – Martijn Pieters Aug 12 '18 at 12:35
  • OK, I will delete the question and look at the linked question which has good answers... well looks like I can't delete it. – user7692855 Aug 12 '18 at 12:36
  • 1
    @MartijnPieters Is this really "too broad"? If it is, shouldn't that java question also be closed for the same reason? – Aran-Fey Aug 12 '18 at 12:38
  • It's not like deciding where to catch an exception requires a lot of decision making or has a dozen edge cases. You catch exceptions whereever you can handle them (and nobody higher up in the call chain is interested in it). End of story. Seems easy enough to communicate in an answer without writing a novel. – Aran-Fey Aug 12 '18 at 12:40
  • I suppose my question is, you want to call a method and if an exception occurs continue with the calling method. Should the calling method or the method called handle the exception in python. Both are capable of handling it. I thought it was an interesting question as when we were discussing it, there was a lot of debate and a clear difference between those who use java versus python. – user7692855 Aug 12 '18 at 12:42
  • There shouldn't be any difference between java and python when it comes to exception handling. Exceptions serve the exact same purpose in all programming languages. – Aran-Fey Aug 12 '18 at 12:44
  • True, but there seems to be a debate in this case. It seemed the ones using java tended to deal with it at the caller method while python dealt with it at the called method. Wasn't sure if there was a defined pythonic way. Both have the same end effect. – user7692855 Aug 12 '18 at 12:47
  • @Aran-Fey: Perhaps, I'll have a look. – Martijn Pieters Aug 12 '18 at 12:51
  • @Aran-Fey: it's now closed as a duplicate. The answer exactly answers the question here: you catch and handle the exception when "the programmer knows what to do", e.g. the application logic dictates that it's the right place. – Martijn Pieters Aug 12 '18 at 12:53
  • @MartijnPieters Thanks! – Aran-Fey Aug 12 '18 at 12:54
  • @user7692855: *any* method can be a called method. Exceptions continue up the stack until caught, you as a programmer get to decide how far. – Martijn Pieters Aug 12 '18 at 12:55
  • @MartijnPieters I think it's best to just delete this question. Maybe I didn't describe the question correctly, or maybe the six developers I was chatting to last night are the only ones who think there is a debate/it's an interesting discussion. – user7692855 Aug 12 '18 at 13:06

1 Answers1

0

Those two pieces of code behave differently: The first one will still try to calculate the tax after fetching the cars failed. If you want this behavior, you essentially need the first version, but if you don't, there is probably no point in having so many try-catch blocks

Markus Unterwaditzer
  • 7,992
  • 32
  • 60