From 3df904b8ab1d50ba4310c619898ed7d0df2b8e52 Mon Sep 17 00:00:00 2001 From: agp8x Date: Tue, 29 Sep 2015 19:39:41 +0200 Subject: [PATCH] improved exception handling --- Setup.py | 2 +- main.py | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Setup.py b/Setup.py index 56403dd..195944b 100644 --- a/Setup.py +++ b/Setup.py @@ -34,7 +34,7 @@ class ConnectionSetup(object): connected_sensors = sensor_setup.setup_sensors() return ipcon, connected_sensors - def disconnect_any(self, connections): + def disconnect_all(self, connections): if connections is None: connections = self.__connections__ for connection in connections: diff --git a/main.py b/main.py index 67b82ca..62533b9 100644 --- a/main.py +++ b/main.py @@ -72,16 +72,23 @@ if __name__ == "__main__": # connection failed, log and exit # TODO: logger.print_exception(inst) log.error("connection failed: " + str(inst)) - # noinspection PyUnboundLocalVariable - input("Press key to restart\n") + try: + input("Press key to restart\n") + except Exception as e: + log.critical("Exception: %s", e) log.info("stop logging... @" + time.ctime() + "\n") - conSetup.disconnect_any(connections) + conSetup.disconnect_all() + connections = [] free_lock() else: log.critical("lock collision: lock 'all' active") log.info("wait for retry (" + str(settings.waitDelay) + ")") time.sleep(settings.waitDelay) except KeyboardInterrupt: - log.info("keyboard-interrupt happened, cleaning up") - conSetup.disconnect_any(connections) + log.info("keyboard-interrupt happened") + except Exception as e: + log.critical("Exception: %s", e) + finally: + log.info("cleaning up") + conSetup.disconnect_all() free_lock()