How to Use RAISE to Simulate a Module/Class Error

I have the following piece of code reading a DS18B20 waterproof sensor

from w1thermsensor import W1ThermSensor, Unit
temp_c = water_sensor.get_temperature()

I suspect, I could encounter the following exceptions documented in help(W1ThermSensor)

raises SensorNotReadyError: if the sensor is not ready yet
raises ResetValueError: if the sensor has still the initial value and no measurment

I can’t figure how how to raise one of these errors to test my TRY/EXCEPT logic. I try the following code

     raise SensorNotReadyError

but my exception code and displayed message show

            exc_type, exc_value, exc_traceback = sys.exc_info()
            summ = str(traceback.extract_tb(exc_traceback))
            if (type == "SensorNotReadyError") or \
               (type == "ResetValueError") :
                print("GET_WATER_READING: runtime error="+type+". Sleeping 2 secs")

            print("GET_WATER_READING: Exception'"+type+"="+str(exc_value)+"'. Traceback: "+summ)
            success = False

GET_WATER_READING: Exception’NameError=name ‘SensorNotReadyError’ is not defined’. Traceback: [<FrameSummary file /home/pi/weathercam/, line 52 in get_water_reading>]

I’m not sure of the correct format of RAISE to use. Any suggestions


First, since this exception is not in the standard exceptions (see Built-in Exceptions — Python 3.10.2 documentation), you have to import it from the package it belongs to (maybe add it to the w1thermsensor import?).

Second, I think you want to raise the exception like this:

raise SystemNotReadyError("some kind of error text here?")  # this line uses the exception, not the rough class!

I don’t have the sensor or the library to test, but you can try this:

import w1thermsensor
raise w1thermsensor.SensorNotReadyError

This should also work:

from w1thermsensor import SensorNotReadyError
raise SensorNotReadyError

Similarly for ResetValueError.

If you need to give arguments to the exceptions, you should be able to
work out what those arguments need to be by inspecting their help:


Hi Diego,

There is no difference between

raise SystemNotReadyError 

raise SystemNotReadyError()

If you raise an exception class, the interpreter automatically creates

an instance from it. The only time you must use the instance form is if

you need to supply arguments:

raise ValueError

raise ValueError()  # exactly the same as above

raise ValueError("bad value")  # supply your own argument
1 Like

Here is my IMPORT

from w1thermsensor import W1ThermSensor, Unit, SensorNotReadyError, ResetValueError

Here are the different combination of RAISE I’ve tried and their associated errors

raise w1thermsensor.SensorNotReadyError
GET_WATER_READING: Exception'NameError=name 'w1thermsensor' is not defined'. Traceback: [<FrameSummary file /home/pi/weathercam/, line 63 in get_water_reading>]
raise W1ThermSensor.SensorNotReadyError
GET_WATER_READING: Exception'AttributeError=type object 'W1ThermSensor' has no attribute 'SensorNotReadyError''. Traceback: [<FrameSummary file /home/pi/weathercam/, line 63 in get_water_reading>]
raise SensorNotReadyError
GET_WATER_READING: Exception'TypeError=__init__() missing 1 required positional argument: 'sensor''. Traceback: [<FrameSummary file /home/pi/weathercam/, line 63 in get_water_reading>]

When I saw this error reply, which indicated I was missing a sensor arguement, I tried the following and it worked. The variable ‘water_sensor’ is the return from calling W1ThermSensor() to connect to the sensor.

raise SensorNotReadyError(water_sensor)
GET_WATER_READING: runtime error=SensorNotReadyError. Sleeping 2 secs
GET_WATER_READING: runtime error=SensorNotReadyError. Sleeping 2 secs
GET_WATER_READING: runtime error=SensorNotReadyError. Sleeping 2 secs

Thanks for the guidance.

Steve, I missed seeing this and the help() would have also point to the need for the sensor. To be honest, I find the output of the help() for an exception very confusing.

The idea that an exception has help? Or the text in the help message?

An exception (or exception class) is an object like any other.

Cameron Simpson

Not all documentation is written well. And yes, some exceptions are especially not well written :frowning:

Also the help() system for Python is aimed at experienced users, it does tend to show a lot more detail than beginners are expecting. The secret here is to learn to ignore most of it!