looking for a custom exception - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: looking for a custom exception (/thread-29811.html) |
looking for a custom exception - JarredAwesome - Sep-21-2020 Hey everyone, I am working on a python script that uses a thermometer. I want to put a line of code in that test to make sure the thermometer is hooked up, so I using "try except". I found a library that seems to do a good job, but the error code that comes back when the thermometer is missing is: w1thermsensor.errors.NoSensorFoundError: Could not find sensor of type DS18B20 with id 0300a27989942 Please check cabling and check your /boot/config.txt for dtoverlay=w1-gpiois there a way I can tell Try ... Except to look for that error in particular? I tried: try: verify code: except w1thermsensor.errors.NoSensorFoundError return resultsAny ideas? Thanks RE: looking for a custom exception - bowlofred - Sep-21-2020 What happens when you try the above? The except line should have a colon, so this doesn't seem runnable.Is verify code: some placeholder? What are you actually calling?First I'd check if the library is throwing something that can actually be caught (as opposed to exiting). If you replace the except with just a bare except: , does it run the exception block?
RE: looking for a custom exception - buran - Sep-21-2020 Also, show as your imports with respect to w1thermsensor or w1thermsensor.errors Looking at the project __init__.py they import their custom errors at to level, so import w1thermsensor try: my_sensor = w1thermsensor.W1ThermSensor() except w1thermsensor.NoSensorFoundError print('No sensor found') RE: looking for a custom exception - JarredAwesome - Sep-21-2020 (Sep-21-2020, 05:25 AM)buran Wrote: Also, show as your imports with respect to Here is my version of that: from w1thermsensor import W1ThermSensor try: sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, '0300a27989945') except w1thermsensor.errors.NoSensorFoundError: print('Thermometer not Found')here is the output: Here is what is in the modules __init__.py filefrom .__version__ import __version__ # noqa from .core import W1ThermSensor # noqa from .errors import NoSensorFoundError # noqa from .errors import SensorNotReadyError # noqa from .errors import UnsupportedUnitError # noqa from .errors import ResetValueError # noqahere is a file called errors.py in the module dir # -*- coding: utf-8 -*- """ This module provides exceptions for the w1thermsensor. """ import textwrap class W1ThermSensorError(Exception): """Exception base-class for W1ThermSensor errors""" pass class KernelModuleLoadError(W1ThermSensorError): """Exception when the w1 therm kernel modules could not be loaded properly""" def __init__(self): super(KernelModuleLoadError, self).__init__( "Cannot load w1 therm kernel modules" ) class NoSensorFoundError(W1ThermSensorError): """Exception when no sensor is found""" def __init__(self, message): super(NoSensorFoundError, self).__init__( textwrap.dedent( """ {} Please check cabling and check your /boot/config.txt for dtoverlay=w1-gpio """.format( message ) ).rstrip() ) class ResetValueError(W1ThermSensorError): """Exception when the reset value is yield from the hardware""" def __init__(self, sensor): super(ResetValueError, self).__init__( "Sensor {} yields the reset value of 85 degree millicelsius. " "Please check the hardware.".format(sensor.id) ) self.sensor = sensorI can't really find where the errors are called RE: looking for a custom exception - buran - Sep-21-2020 (Sep-21-2020, 05:48 PM)JarredAwesome Wrote: Here is what is in the modules __init__.py fileI know what's in __init__.py - that's why I show it to you. In my code I import the module w1thermsensor and then fully reference the W1ThermSensor and NoSensorFoundError If you prefer from w1thermsensor import W1ThermSensor , then it should befrom w1thermsensor import W1ThermSensor, NoSensorFoundError try: sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, '0300a27989945') except NoSensorFoundError: print('Thermometer not Found') RE: looking for a custom exception - JarredAwesome - Sep-21-2020 (Sep-21-2020, 05:53 PM)buran Wrote: I know what's in Sorry about that. I missread what you wrote. That updated code works perfected! thanks. |