9

I am original Java developer, for me, checked Exception in Java is obviously/easy enough for me to decide to catch or throw it to the caller to handle later. Then it comes Python, there is no checked exception, so conceptually, nothing forces you to handle anything(In my experience, you don't even know what exceptions are potentially thrown without checking the document). I've been hearing quite a lot from Python guys that, in Python, sometimes you better just let it fail at runtime instead of trying to handle the exceptions.

Can someone give me some pointers regarding to:

  1. what's the guideline/best practice for Python Exception Handling?

  2. what's the difference between Java and Python in this regard?

2
  • 3
    Not only nothing forces you to handle anything, but finding documentation on what exceptions are thrown when is a royal PITA :/
    – fge
    Commented Jun 17, 2013 at 23:03
  • @fge yes, exactly, it is quite annoying.
    – Shengjie
    Commented Jun 17, 2013 at 23:06

3 Answers 3

2

OK, I can try and give an answer which I'll keep as neutral as it can be... (note: I have done Python professionally for a few months, but I am far from mastering the language in its entirety)

  1. The guidelines are "free"; if you come from a Java background, you will certainly spend more time than most Python devs out there looking for documentation on what is thrown when, and have more try/except/finally than what is found in regular python code. In other words: do what suits you.

  2. Apart from the fact that they can be thrown anywhere, at any moment, Python has multi-exception catch (only available in Java since 7), with (somewhat equivalent to Java 7's try-with-resources), you can have more than one except block (like Java can catch more than once), etc. Additionally, there are no real conventions that I know of on how exceptions should be named, so don't be fooled if you see SomeError, it may well be what a Java dev regards as a "checked exception" and not an Error.

0

I'm not a Python dev, but I've done a quite bit of work in C#, which also doesn't have checked exceptions. Being a Java coder that took some getting used to. I personally still think checked exceptions are a nice feature, but opinions differ wildly (as you can see in some of the responses here).

There are quite a few articles online on why checked exceptions are(n't) a good idea (see this blog for one such opinion).

But regardless of your preference: the rule of thumb in Python and C# is to do essentially what you're already doing - test runs and reading the docs.

What I'd typically do in C# is have a 'catch-all' exception handler at the root of my program that makes sure any error is reported and the program exits cleanly, and then deeper in my code have more specific exception handlers for "known specific errors". So actually, not so different from how you'd work in Java, just a little more work to figure out where to put your specific handlers.

2
  • @fge, in fact, as far as I know, they're very much a Java-specific feature. Commented Jun 17, 2013 at 23:49
  • Well, for starters it has your opinions in it.
    – Marcin
    Commented Jun 20, 2013 at 19:16
-4
  1. Best practice is to handle appropriate exceptions in the appropriate place. Only you, as developer, can decide which part of your code should catch exceptions. This should become apparent with decent unit testing. If you have unhandled exceptions, they will show up.

  2. You already described the differences. At a more fundamental level, Java's designers think they know better than you how you should code, and they'll force you to write lots of it. Python, by contrast, assumes that you are an adult, and that you know what you want to do. This means that you can shoot yourself in the foot if you insist on so doing.

16
  • 4
    The second point is a personal opinion and by no means objective.
    – fge
    Commented Jun 17, 2013 at 23:20
  • The difference is not about "Java's designer thinking they know better". Checked exceptions are simply a language feature that can inform you as a coder at compile time, rather than at runtime, that a problem might occur. It's fine if you don't like that feature, but honestly, lose the attitude. Commented Jun 17, 2013 at 23:23
  • I'd appreciate the answer from the neutral/middle zone between Java and Python.
    – Shengjie
    Commented Jun 17, 2013 at 23:23
  • @Shengjie What is this imaginary neutral middle zone?
    – Marcin
    Commented Jun 18, 2013 at 11:31
  • 1
    @Marcin I'm sorry but no. You say that "Java's designers think they know better than you", I am sorry but that is as one-sided a statement as a statement can be. You don't like it, OK. Generalizing your dislike to what Java language designers thought stems from your personal point of view only.
    – fge
    Commented Jun 18, 2013 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.