There is a view code

try: a = 3 / x except ZeroDivisionError: pass try: b = 3 / y except ZeroDivisionError: pass try: c = 3 / z except ZeroDivisionError: pass 

those. an error in one calculation should not interfere with the rest. In VBA, it would be possible to set the On Error Resume Next command before calculations. Is it possible to make this code somehow easier in python?

Or is it generally better to use conditional operators instead of exceptions?

UPD

During the discussion, it turned out that the real task still affects the answer. I use openpyxl .

 try: _ = ws.cell(row=i, column=required_columns.pp, value=pp) except ValueError: pass try: _ = ws.cell(row=i, column=required_columns.sum, value=sum) except ValueError: pass 

where required_columns is a namedtuple containing the numbers of the columns to which the data is output. If the column is not needed, then its value is 0, and this try just skips it. The return value is not used anywhere.

  • Something like "Resume Next" in Python is not. What should you write to a variable if the dividend is zero? You can do something like this (at zero x, None will fall into a): a = 3 / x if x else None - insolor
  • The @insolor code simply shows a general meaning, in reality certain actions are performed there, but the meaning is clear. I'll wait, maybe someone else will offer something, and you can arrange it as an answer. - Edward Izmalkov
  • Why in your case instead of "pass" you can not continue to execute the code? - Igor
  • @Igor did not understand the question. You suggest the following try place under except ? Then generally it will not be what is needed. - Edward Izmalkov
  • rather, closer to recursion, instead of pass, we put a function call or continue calculations, depending on the task. Nesting should not be. - Igor

2 answers 2

From The Zen of Python :

Errors should never pass silently.
Unless explicitly silenced.

Transfer:

Errors should never be ignored.
Unless explicitly ignored.

To ignore the exception that occurred in the code block, you can use contextlib.suppress() :

 from contextlib import suppress with suppress(SomeError): some_action() may_raise_some_error() run_anyway() 

run_anyway() is executed even if some_action() or may_raise_some_error() throw a SomeError exception. If some_action() throws an exception, then may_raise_some_error() not executed.

In your case:

 with suppress(ValueError): ws.cell(row=i, column=required_columns.sum, value=sum) 

For the ZeroDivisionError example, an explicit condition check, as shown by @insolor, may be more preferable:

 a = 3 / x if x else None # a is None for falsy x a = x and 3 / x # a is x for falsy x if x: #XXX AVOID IT a = 3 / x # NameError for falsy x 

If you know the conditions under which you want to miss a call:

 if required_columns.sum: ws.cell(row=i, column=required_columns.sum, value=sum) 

This may be preferable, especially if the ws.cell() call may have undesirable side effects (creating a new cell).

Sometimes an exception is preferable:

 with suppress(FileNotFoundError): os.remove(filename) 

This does not suppress access errors and performs one IO action instead of two if successful ( os.path.exists() also implemented through exceptions).

  • An interesting feature with suppress , and I used to create / delete if I first check if it exists / is missing - gil9red

Something similar to "Resume Next" in Python is not (and well what not). "Resume Next" is beautiful to everyone, except that you can forget to handle some kind of exception, as a result, the logic of the program may break, although no errors will be displayed.

Alternatively, you can directly in the calculation to check whether the dividend is zero:

 a = 3 / x if x else None b = 3 / y if y else None c = 3 / z if z else None 

As a result, if one of the dividends is zero, then None is written into the variable. After all the calculations, you will need to check the result of the calculations on is None , and somehow handle this case.

  • alternatively: a = x and 3 / x (can be used, if allowed, that a is x in the case of falsy x ). Maybe “kind-of” monad in python - jfs
  • @jfs, I thought about using and , but then the result will be 0, and it will be difficult to distinguish it from the correct completion of the calculations. Although, of course, it all depends on the specific task. - insolor
  • yes, of course, that's why I wrote above: “if it is acceptable” - jfs
  • In principle, it is permissible. I think it was necessary to indicate in the question what I am doing, because it affects the answers. now then I will add a question - Edward Izmalkov