Raising my own Exception in Python 2.7 -


i copied , pasted these lines of code pyhton tutorial book. why code not work when try run in pycharm?

def inputnumber ():     x = input ('pick number: ')     if x == 17:       raise 'badnumbererror', '17 bad number'     return x inputnumber() 

this got when run code:

pick number: 17 traceback (most recent call last):   file "c:/users/arman/desktop/scribble/hello.py", line 153, in <module>     inputnumber()   file "c:/users/arman/desktop/scribble/hello.py", line 151, in inputnumber     raise 'badnumbererror', '17 bad number' typeerror: exceptions must old-style classes or derived baseexception, not str 

you can use standard exceptions:

raise valueerror('17 bad number') 

or can define own:

class badnumbererror(exception):     pass 

and use it:

raise badnumbererror('17 bad number') 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -