title: In a unit test, how can the parameter passed to a custom exception be determined?
body: <pre><code>class AppError(Exception): pass
  
  class MissingInputError(AppError):
  
      em = {1101: "Date input is missing. Please verify.", \
            1102: "Key input is missing. Please verify.", \
            1103: "Stn input is missing. Please verify."}
            # and so on ...
  </code></pre>
  
  <p>...</p>
  
  <pre><code>def validate(self):
      """ Method of Input class to validate input and save it """
  
      params = self.__params
  
      if 'dt' in params:
          self.__validateKey(escape(params['dt'][0]))
      else:
          raise MissingInputError(1101)
  
      if 'key' in params:
          self.__validateService(escape(params['key'][0]))
      else:
          raise MissingInputError(1102)
  
      # and so on ...
  </code></pre>
  
  <p>Unit testing the above, I know that the following tests in the MissingInput test class:</p>
  
  <pre><code>def testMissingKeyInput(self):
      """ Missing key should raise error """
      ip = controller.Input(MissingInput.missInputKey)
      self.assertRaises(errors.MissingInputError, ip.validate)
  
  def testMissingDtInput(self):
      """ Missing dt should raise error """
      ip = controller.Input(MissingInput.missInputDt)
      self.assertRaises(errors.MissingInputError, ip.validate)
  
  # and so on ...
  </code></pre>
  
  <p>will correctly detect if a MissingInputError exception was raised. </p>
  
  <p>Is there any way to determine in the test what error number was passed to the exception while calling it, so that I can be sure that the error is being raised for that particular missing input, and not for any other missing inputs?</p>
  
  <p>(P.S: <strong>Python 2.4.3</strong>).</p>
  
  <hr>
  
  <p><strong>Tip</strong>: If you are stuck with 2.4 to 2.6, <strong>use the <a href="http://www.voidspace.org.uk/python/modules.shtml#unittest2" rel="nofollow">unittest2 library</a></strong>.
  In Python 2.7 and 3.2 a whole bunch of improvements to unittest will arrive. unittest2 is a backport of the new features (and tests) to work with Python 2.4, 2.5 &amp; 2.6.</p>