diff --git a/slic/utils/__init__.py b/slic/utils/__init__.py index 077e75558..33dfd318a 100644 --- a/slic/utils/__init__.py +++ b/slic/utils/__init__.py @@ -5,7 +5,7 @@ from .channels import load_channels, Channels from .config import Config from .elog import Elog from .eval import arithmetic_eval -from .exceptions import ChainedException, printable_exception +from .exceptions import ChainedException, printable_exception, printed_exception from .ipy import devices from .json import json_save, json_load from .marker import Marker, markers diff --git a/slic/utils/exceptions.py b/slic/utils/exceptions.py index c57b26542..1b51e8db8 100644 --- a/slic/utils/exceptions.py +++ b/slic/utils/exceptions.py @@ -1,4 +1,5 @@ -from .utils import typename +from contextlib import AbstractContextManager +from .utils import typename, singleton class ChainedException(Exception): @@ -22,3 +23,15 @@ def printable_exception(exc): +@singleton +class printed_exception(AbstractContextManager): + + def __exit__(self, exc_type, exc_val, exc_tb): + if exc_type is not None: + name = exc_type.__name__ + message = exc_val or "" + print("{}: {}".format(name, message)) + return True # this causes the with statement to suppress the exception + + +