Raising my own Exception in Python 2.7
<p>I copied and pasted these lines of code from a Pyhton tutorial book. Why does this code not work when I try to run it in PyCharm?</p>

<pre><code>def inputNumber ():
    x = input ('Pick a number: ')
    if x == 17:
      raise 'BadNumberError', '17 is a bad number'
    return x
inputNumber()
</code></pre>

<p>This is what I got when I run the code:</p>

<pre><code>Pick a number: 17
Traceback (most recent call last):
  File "C:/Users/arman/Desktop/Scribble/Hello.py", line 153, in &lt;module&gt;
    inputNumber()
  File "C:/Users/arman/Desktop/Scribble/Hello.py", line 151, in inputNumber
    raise 'BadNumberError', '17 is a bad number'
TypeError: exceptions must be old-style classes or derived from BaseException, not str
</code></pre>

<p>Just inherit from <code>Exception</code> class, then you can throw your own exceptions:</p>

<pre><code>class BadNumberException(Exception):
    pass

raise BadNumberException('17 is a bad number')
</code></pre>

<p>output:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;module1&gt;", line 4, in &lt;module&gt;
BadNumberException: 17 is a bad number
</code></pre>

python