Try excluding and programming etiquette

I am creating a GUI and I have found that I am using a lot of try except try . My question is, should I redesign my program to use try except or try except try good practice used in python programs? I like them because they are informative and make debugging easier for me. Just wondering what the real developers think about it.

thanks

+4
source share
3 answers

One of Python's idioms: It's easier to ask forgiveness than permission. (Python Glossary, look at EAFP).

Therefore, it is quite acceptable to structure a program stream with exception handling (and quickly enough compared to other languages). This is consistent with the dynamic nature of Python imho well.

+7
source

One important consideration when deciding whether to catch an exception is that you could hide legitimate errors.

For example, consider this code:

 try: name = person['name'] except KeyError: name = '<none provided>' 

This is reasonable if person known as a dict ... But if person can be something more complex, for example:

 class Person(object): def __getitem__(self, key): return do_something(key) 

You run the risk of accidentally catching an exception that was caused by a legitimate error (for example, an error in do_something ).

And I feel the need to mention: you should never, ever (with the exception of a few special circumstances) use "naked" except:

My personal preference is to avoid catching exceptions whenever possible (e.g. using name = person.get('name', '<none provided>') ), since I find it cleaner and I don't like it look of try / catch blocks.

+4
source

It's hard to give a general answer about whether to use less exception handling ... you can definitely do too much and too little. It is almost certainly incorrect to catch all possible exceptions, and it is also almost certainly incorrect to do exception handling.

Here are some things to think about:

  • It is usually useful to catch an exception if you can do something programmatically with the error condition. For instance. your code is trying to make a web request, and if it does not work, you want to try again. In this situation, you want to catch the exception, and then try again.
  • Think about where to catch the exception. In some low level function, can you intelligently do something about the error? For instance. let's say you have a function that writes a file and it fails with a permission error. You probably can't do it there, but maybe at a higher level, you can catch the exception and display a message to the user who asks them to try to save the file elsewhere.
  • It almost never makes sense to catch "fatal" types of errors, for example. due to lack of memory, etc. At least not a low level in your code - it might make sense to have a top-level handler that tries to gracefully exit.
  • Do not allow the β€œswallowing” of exceptions that should really bubble, i.e. don't have an except clause that doesn't throw an exception if your calling function really needs to see it. This can hide serious errors.

Also, do a Google search for "exception handling rules." Many of the results you see will be for other languages ​​/ environments, but these concepts are also applicable.

+2
source

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


All Articles