3

I guess I'm not the first asking this question, but I haven't found a solution that I could use/understand yet. And the issue is probably not as simple as i first expected.

I think it can be boiled down to two general questions:

1) Is there a way to avoid Python to stop when an error occur and just jump on to the next line of code in the script?

2) Is there a way to make Python execute a line of code if an error occurs? Like, if error then...

My concrete problem: I have a very large program with a lot of functions and other stuff, which would take forever to adjust individually by using "try" for example (if i understand it correctly)

My program run as a large loop that gather information and keeps running. This means that it does not really matter to me, that my program fails multiple time as long as it keeps running. I can easily handle that some of the information is with error and would just like my program to take a note of it and keep going.

Is there a solution to this?

AndersBL
  • 59
  • 1
  • 4
  • 2
    Possible duplicate of [On Error Resume Next in Python](https://stackoverflow.com/questions/26059424/on-error-resume-next-in-python) – fractals Aug 21 '18 at 07:33
  • Have you even tried anything? Ever had a look at [Python Docs - Error Handling](https://docs.python.org/3/tutorial/errors.html) ? – Igl3 Aug 21 '18 at 07:36

2 Answers2

2

As you rightly pointed out, the try/catch block in Python is by far your best ally:

for i in range(N):
    try: do_foo()  ; except: do_other_foo()
    try: do_bar()  ; except: do_other_bar()

Alternatively, you could also use, in case you didn't need the Exception:

from contextlib import suppress

for i in range(N):
    with suppress(Exception):
        do_foo()
    with suppress(Exception):
        do_bar()
ibarrond
  • 6,617
  • 4
  • 26
  • 45
2

Your only possibility is to rely on the try/except clause. Keep in mind that the try/except may use also finally and else (see documentation:

try:
    print("problematic code - error NOT raised")
except:
    print("code that gets executed only if an error occurs")
else:
    print("code that gets executed only if an error does not occur")
finally:
    print("code that gets ALWAYS executed")
# OUTPUT:
# problematic code - error NOT raised
# code that gets executed only if an error does not occur
# code that gets ALWAYS executed

or, when an error is raised:

try:
    print("problematic code - error raised!")
    raise "Terrible, terrible error"
except:
    print("code that gets executed only if an error occurs")
else:
    print("code that gets executed only if an error does not occur")
finally:
    print("code that gets ALWAYS executed")
# OUTPUT:
# problematic code - error raised!
# code that gets executed only if an error occurs
# code that gets ALWAYS executed

I urge to point out, by the way, that ignoring everything makes me shiver:
you really should (at least, more or less) identify which exception can be raised, catch them (except ArithmeticError: ..., check built-in exceptions) and handle them individually. What you're trying to do will probably snowball into an endless chain of problems, and ignoring them will probably create more problems!

I think that this question helps to understand what a robust software is, meanwhile on this one you can see how SO community thinks python exceptions should be handled

Gsk
  • 2,929
  • 5
  • 22
  • 29
  • else clause is for code that didn't raise any error in the try, from python documentation: "The try … except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception." https://docs.python.org/3/tutorial/errors.html – Matthieu Dsprz Apr 23 '21 at 13:11
  • @MatthieuDsprz Thank you for pointing out the copy-pasting error; it was definitively confusing for a new programmer, despite the output and the possibility to try out the code. Fixed it! – Gsk Apr 23 '21 at 13:48