code: def testExceptionHandling(self):
      ctx = GReqCtx()
  
      def raise_mpi(*args):
          raise MPI.Exception(MPI.ERR_BUFFER)
  
      def raise_rte(*args):
          raise ValueError(42)
  
      def check_exc(exception, is_mpi, stderr):
          output = stderr.getvalue()
          header = "Traceback (most recent call last):\n"
          if is_mpi:
              chkcode = MPI.ERR_BUFFER
              excname = MPI.Exception.__name__
          else:
              chkcode = MPI.ERR_OTHER
              excname = ValueError.__name__
          ierr = exception.Get_error_code()
          self.assertEqual(ierr, chkcode)
          self.assertTrue(output.startswith(header))
          self.assertIn(excname, output)
  
      for raise_fn, is_mpi in (
          (raise_mpi, True),
          (raise_rte, False),
      ):
          greq = MPI.Grequest.Start(raise_fn, ctx.free, ctx.cancel)
          greq.Complete()
          with self.assertRaises(MPI.Exception) as exc_cm:
              with unittest.capture_stderr() as stderr:
                  greq.Wait()
          if greq:
              greq.Free()
          check_exc(exc_cm.exception, is_mpi, stderr)
          #
          greq = MPI.Grequest.Start(ctx.query, raise_fn, ctx.cancel)
          greq.Complete()
          with self.assertRaises(MPI.Exception) as exc_cm:
              with unittest.capture_stderr() as stderr:
                  greq.Wait()
          if greq:
              greq.Free()
          check_exc(exc_cm.exception, is_mpi, stderr)
          #
          greq = MPI.Grequest.Start(ctx.query, ctx.free, raise_fn)
          with self.assertRaises(MPI.Exception) as exc_cm:
              with unittest.capture_stderr() as stderr:
                  greq.Cancel()
          greq.Complete()
          greq.Wait()
          if greq:
              greq.Free()
          check_exc(exc_cm.exception, is_mpi, stderr)
  
file_path: mpi4py/test/test_grequest.py
imports: from mpi4py import MPI
  import mpiunittest
label_desc: Use image node_id 2 for calling the TestGrequest obj's underlying member method code with example usage: obj.testExceptionHandling() without return types