TransWikia.com

How to ignore errors but continue looking for errors

Stack Overflow Asked by lek on January 9, 2021

I want to run code that will sometimes have errors and sometimes won’t have errors. Here’s an example:

try:
    errorexample
except Exception:
    pass
    try:
        print("print1")
    except Exception:
        pass
        try:
            print("print2")
        except Exception:
            pass

When I run that, the message that comes back is
"print1". The problem is that it is only printing "print1" when I want it to print "print1" and "print2"

3 Answers

You need to move your second 'try...except' block out of the first 'try...except' - at the moment if there is no first exception, it doesn't get reached

e.g., if you want to attempt print("print2") regardless of whether print("print1") throws an exception or not, you need to move it out of the block like this

try:
    errorexample
except Exception:
    pass
    try:
        print("print1")
    except Exception:
        pass
    try:
        print("print2")
    except Exception:
        pass

Correct answer by MGo on January 9, 2021

If the code has not an another error it will not reach the second statement, and so it will not print "print2"

Answered by Federico on January 9, 2021

That's because your except block in line 7 is never executed since the try block in line 5 doesn't raise any errors.

Answered by Rayquados on January 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP