title: python cannot use exception fields in handler
body: <p>I building an assembler for a class assignment. What my program does is read files than spit out machine code, (after striping comments etc... but that all works).</p>
  
  <p>What I'm tying to do is build an exception handler on top of all this so that it will catch exceptions thrown by bad code and I can handle them by printing out the line where the bad code was so it's easy to debug bad code.</p>
  
  <p>My problem is that I can't figure out how to access the exception instance that was caught. Everything I find seems to give me the class object for the exception in particular.</p>
  
  <p>Right now I getting errors like this:</p>
  
  <pre><code>AttributeError: 'UnknownCommandException' object has no attribute 'line'
  </code></pre>
  
  <p>Here is the top level program I'm handling the exceptions in now:</p>
  
  <pre><code>import tkinter as tk
  from tkinter.filedialog import *
  from assembler import readCodeFromFile
  from exceptions import UndeclaredLabelException
  from exceptions import UnknownCommandException
  from exceptions import RegisterNotFoundException
  
  def main():
      readFileStr = tk.filedialog.askopenfilename()
      wrtFile = readFileStr.replace(".txt","") + "ASSEMBLY.txt"
      try:
          readCodeFromFile(readFileStr, wrtFile, base="*")
      except UndeclaredLabelException as e:
          print("undeclared label '{}' near line {}".format(e.label, e.line)) 
          print(e)
      except UnknownCommandException as e:
          print("unknown command '{}' near line {}".format(e.inst, e.line))
          print(e)
      except RegisterNotFoundException as e:
          print("unknown reg '{}' near line {}".format(e.reg, e.line))
          print(e)
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>I also tied to use <code>sys.exc_info()</code> which did not work for me either.</p>
  
  <p>The code where the exceptions are defined.</p>
  
  <pre><code>class RegisterNotFoundException(Exception):
      """this exception is thrown when a string does not match any of the known registers for this language."""
      def __init__ (self, reg, line):
          self.reg = reg
          self.line = line   
  class UnknownCommandException(Exception):
      """this exception is thrown when a nonexistant command is used."""
      def __init__(self, inst, line):
          self.inst = inst
          self.lineNumber = line
  class UndeclaredLabelException(Exception):
      """this exception is thrown when a label is used but has not been declared."""
      def __init__(self, label, line):
          self.badLabel = label
          self.lineNumber = line
      def __repr__ (self):
          return "bad label: '" + self.badLabel + "'"
  </code></pre>
  
  <p>I've found that this code handles my <code>RegisterNotFoundException</code> just fine, but not the other two, which leaves me even more confused than before.    </p>