0

I'm trying to throw a custom error when incorrect input is used to create an object, but am getting this error when trying to raise the exception.

TypeError: exceptions must derive from BaseException

Here is the relevant code I'm working with

def UnitError(Exception):
    pass

def ValueError(Exception):
    pass

class Temperature():

    def __init__(self, temp = 0.0, unit = 'C'):

        if type(temp) != int:
            raise ValueError('TEST') #ERROR occurs here
        else:
            self.t = float(temp)

        self.u = unit.upper()

I've never come across this error when raising custom exceptions before, could someone explain what's going on here, and how I could fix it?

1 Answer 1

7

Your "exceptions" are functions, not classes.

Rewrite them in the following way:

class UnitError(Exception):
    pass
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.