Не завершать приложение при ошибке

Как сделать так, что бы приложение не останавливалось после ошибки?


Ответы (1 шт):

Автор решения: vadim vaduxa
import contextlib, random, sys

def before():
    raise AssertionError

def main(a):
    if a == 1:
        raise KeyError(a)
    elif a == 2:
        raise FileNotFoundError(a) if random.randint(0, 1) else FileExistsError
    else:
        raise FileNotFoundError(a)

def after():
    raise AssertionError if random.randint(0, 1) else UserWarning

if __name__ == '__main__':
    with contextlib.suppress(AssertionError):
        before()
    with contextlib.suppress(Exception):
        main(1)
    try: main(2)
    except (FileNotFoundError, FileExistsError):
        try: main(3)
        except: print(sys.exc_info())
    except Exception as e: print(e)
    else: print('no err')
    finally:
        with contextlib.suppress(AssertionError, UserWarning):
            after()
    print('end')
    sys.exit(0)
→ Ссылка