Python Forum
looking for a custom exception
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looking for a custom exception
#4
(Sep-21-2020, 05:25 AM)buran Wrote: 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')

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:
Error:
Traceback (most recent call last): File "curl.py", line 5, in <module> sensor = w1ThermSensor(w1ThermSensor.THERM_SENSOR_DS18B20, '0300a27989945') NameError: name 'w1ThermSensor' is not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "curl.py", line 6, in <module> except w1thermsensor.errors.NoSensorFoundError: NameError: name 'w1thermsensor' is not defined
Here is what is in the modules __init__.py file
from .__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  # noqa
here 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 = sensor
I can't really find where the errors are called
Reply


Messages In This Thread
looking for a custom exception - by JarredAwesome - Sep-21-2020, 02:26 AM
RE: looking for a custom exception - by bowlofred - Sep-21-2020, 04:00 AM
RE: looking for a custom exception - by buran - Sep-21-2020, 05:25 AM
RE: looking for a custom exception - by JarredAwesome - Sep-21-2020, 05:48 PM
RE: looking for a custom exception - by buran - Sep-21-2020, 05:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  custom exception dcr 1 390 Feb-17-2024, 08:19 PM
Last Post: Gribouillis
  problem using custom exception handling in python srm 3 3,133 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  During handling of the above exception, another exception occurred Skaperen 7 27,109 Dec-21-2018, 10:58 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020