Exception Management Guide - Python vs Java

I am the original Java developer, it has been verified for me that the exception in Java is explicitly / simple enough for me to decide whether to catch or throw it to the caller to handle later. Then comes Python, an excluded exception is thrown, so conceptually nothing forces you to process anything (in my experience, you don’t even know which exceptions are potentially thrown without checking the document). I heard quite a lot from the Python guys that in Python sometimes you better just let it fail at runtime rather than trying to handle exceptions.

Can someone give me some pointers regarding:

  • What is the guide / best practice for handling Python exceptions?

  • What is the difference between Java and Python in this regard?

+6
source share
3 answers

Well, I can try to give an answer that I keep as neutral as it can be ... (note: I worked Python professionally for several months, but I am far from learning the language as a whole)

  • The guidelines are β€œfree”; if you come from a Java background, you will certainly spend more time than most Python developers looking for documentation of what throws when there is, and have more try / except / finally than what is found in regular python code . In other words: do what suits you.

  • Besides the fact that they can be thrown anywhere, at any time Python has a catch with several exceptions (available only in Java since 7), with (somewhat equivalent to Java 7 try-with-resources), you can have more than one except block ( for example, Java can catch more than once), etc. In addition, there are no real conventions that I know about how to specify exceptions, so don't be fooled if you see SomeError , it is entirely possible that the Java developer sees it as a "checked exception" rather than Error .

+1
source

I am not a Python developer, but I did quite a bit of work in C #, which also did not check for exceptions. He was the Java encoder that he was used to. I personally still think that checked exceptions are a great feature, but opinions vary widely (as you can see in some answers here).

There are many articles on the Internet about why checked exceptions are (and not) a good idea (see this blog post for one such opinion).

But whatever your preference: the rule of thumb in Python and C # is to do what you are already doing β€” running tests and reading documents.

What I usually did in C # has a catch-all exception handler in the root of my program that allows me to report any error, and the program crashes, and then more deeply in my code has a more specific exception handlers for "known specific errors. " So, in fact, it’s not so different from how you work in Java, just a little more work to figure out where to place your specific handlers.

0
source
  • It is best practice to handle the relevant exceptions in the appropriate place. Only you, as a developer, can decide how much of your code should catch exceptions. This should become apparent with decent unit testing. If you have unhandled exceptions, they will appear.

  • You have already described the differences. On a more fundamental level, Java developers believe that they know better than you how you should code, and they will make you write a lot. Python, by contrast, assumes that you are an adult and that you know what you want. This means that you can shoot in the foot if you insist on it.

-3
source

Source: https://habr.com/ru/post/947509/


All Articles