2016-02-03 94 views
0

我试图抛出一个自定义错误,当不正确的输入用于创建一个对象,但当试图引发异常时得到此错误。TypeError调用自定义异常时

TypeError: exceptions must derive from BaseException 

下面是相关的代码我与

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() 

之前提高自定义异常时,我从来没有碰到过这样的错误工作,可能有人解释一下是怎么回事,我怎么可能修理它?

回答

5

你的“例外”是功能,而不是类。

重写它们以下列方式:

class UnitError(Exception): 
    pass 
+0

啊,很无聊的我。谢谢! – 23k

+0

@ 23k,欢迎您! – soon