title: Python: Unit Test That Asserts Custom Exception Is Raised Fails When Try-Except Is Involved
body: <p>I'm trying to write a unit test that asserts that my nested custom exception is raised by a function.</p>
  <p>The below sample code passes:</p>
  <pre><code>from unittest import TestCase
  
  
  class MyClass():
      class MyException(Exception):
          pass
  
  
  def fail():
      raise MyClass.MyException()
  
  
  class MyTests(TestCase):
      def test_throwsException(self):
          with self.assertRaises(MyClass.MyException):
              fail()
  </code></pre>
  <p>However, when my raising code involves a try-except, my test fails:</p>
  <pre><code>from unittest import TestCase
  from enum import Enum
  
  
  class Weekdays(Enum):
      MONDAY = 'mon'
      TUESDAY = 'tue'
      WEDNESDAY = 'wed'        
      THURSDAY = 'thu'        
      FRIDAY = 'fri'        
  
      class InvalidValue(Exception):
          pass
  
  
  def parse(key: str) -&gt; Weekdays:
      try:
          return Weekdays(key)
      except Exception as e:
          raise Weekdays.InvalidValue() from e
  
  
  class MyTests(TestCase):
      def test_throwsException(self):
          with self.assertRaises(Weekdays.InvalidValue):
              parse('invalid')
  </code></pre>
  <p>It returns the below error:</p>
  <pre><code>E
  ======================================================================
  ERROR: test_throwsException (test_main.MyTests)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File &quot;E:\PythonCodes\playground\test_main.py&quot;, line 25, in test_throwsException
      with self.assertRaises(Weekdays.InvalidValue):
    File &quot;C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\unittest\case.py&quot;, line 816, in assertRaises
      return context.handle('assertRaises', args, kwargs)
    File &quot;C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\unittest\case.py&quot;, line 187, in handle
      raise TypeError('%s() arg 1 must be %s' %
  TypeError: assertRaises() arg 1 must be an exception type or tuple of exception types
  
  ----------------------------------------------------------------------
  Ran 1 test in 0.001s
  
  FAILED (errors=1)
  </code></pre>
  <p>I don't quite understand what <code>arg 1 must be an exception type</code> means because I assume that my custom exception <em><strong>is</strong></em> an exception type.</p>
  <p>Why is the second version with the try-except failing?</p>