detecting if no exception was thrown

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aryeh M. Friedman

    detecting if no exception was thrown

    I am writting a uit tsting framework and need to know is there any way to
    detect if some expression throws a exception or not (I know how to handle
    the case of it throwing but not the expected one). Here is my test macro so
    far:

    #define TEST_ASSERT_THR OW(expr,x) \
    try { expr; } catch(x) {test_pass(0);} catch(...) {test_fail(0);} ;

    What I want is a test_fail(0) if no exception was thrown by expr.

    --Aryeh


  • Clark S. Cox III

    #2
    Re: detecting if no exception was thrown

    On 2005-02-18 23:17:55 -0500, "Aryeh M. Friedman"
    <aryeh@m-net.arbornet.or g> said:
    [color=blue]
    > I am writting a uit tsting framework and need to know is there any way
    > to detect if some expression throws a exception or not (I know how to
    > handle the case of it throwing but not the expected one). Here is my
    > test macro so far:
    >
    > #define TEST_ASSERT_THR OW(expr,x) \
    > try { expr; } catch(x) {test_pass(0);} catch(...) {test_fail(0);} ;
    >
    > What I want is a test_fail(0) if no exception was thrown by expr.[/color]

    How about:

    #define TEST_ASSERT_THR OW(expr,x) \
    { \
    bool caught_exceptio n = false; \
    try { expr; } catch(x) {caught_excepti on = true;} \
    if(caught_excep tion) \
    test_pass(0); \
    else \
    test_fail(0); \
    };

    --
    Clark S. Cox, III
    clarkcox3@gmail .com

    Comment

    • Kurt Stutsman

      #3
      Re: detecting if no exception was thrown

      Clark S. Cox III wrote:[color=blue]
      > On 2005-02-18 23:17:55 -0500, "Aryeh M. Friedman"
      > <aryeh@m-net.arbornet.or g> said:
      >[color=green]
      >> I am writting a uit tsting framework and need to know is there any way
      >> to detect if some expression throws a exception or not (I know how to
      >> handle the case of it throwing but not the expected one). Here is my
      >> test macro so far:
      >>
      >> #define TEST_ASSERT_THR OW(expr,x) \
      >> try { expr; } catch(x) {test_pass(0);} catch(...)
      >> {test_fail(0);} ;
      >>
      >> What I want is a test_fail(0) if no exception was thrown by expr.[/color]
      >
      >
      > How about:
      >
      > #define TEST_ASSERT_THR OW(expr,x) \
      > { \
      > bool caught_exceptio n = false; \
      > try { expr; } catch(x) {caught_excepti on = true;} \
      > if(caught_excep tion) \
      > test_pass(0); \
      > else \
      > test_fail(0); \
      > };
      >[/color]

      Or if you know that test_pass(0) doesn't throw x:

      try {
      expr;
      test_pass(0);
      } catch(x) {
      test_fail(0);
      }

      Comment

      • Aryeh M. Friedman

        #4
        Re: detecting if no exception was thrown


        "Kurt Stutsman" <kstutsman@NOSP AM.sbcglobal.ne t> wrote in message
        news:SNzRd.1350 $DC6.1236@newss vr14.news.prodi gy.com...[color=blue]
        > Clark S. Cox III wrote:[color=green]
        >> On 2005-02-18 23:17:55 -0500, "Aryeh M. Friedman"
        >> <aryeh@m-net.arbornet.or g> said:
        >>[color=darkred]
        >>> I am writting a uit tsting framework and need to know is there any way
        >>> to detect if some expression throws a exception or not (I know how to
        >>> handle the case of it throwing but not the expected one). Here is my
        >>> test macro so far:
        >>>
        >>> #define TEST_ASSERT_THR OW(expr,x) \
        >>> try { expr; } catch(x) {test_pass(0);} catch(...)
        >>> {test_fail(0);} ;
        >>>
        >>> What I want is a test_fail(0) if no exception was thrown by expr.[/color]
        >>
        >>
        >> How about:
        >>
        >> #define TEST_ASSERT_THR OW(expr,x) \
        >> { \
        >> bool caught_exceptio n = false; \
        >> try { expr; } catch(x) {caught_excepti on = true;} \
        >> if(caught_excep tion) \
        >> test_pass(0); \
        >> else \
        >> test_fail(0); \
        >> };
        >>[/color]
        >
        > Or if you know that test_pass(0) doesn't throw x:
        >
        > try {
        > expr;
        > test_pass(0);
        > } catch(x) {
        > test_fail(0);
        > }[/color]

        Actually the test only passes if it throws x

        --Aryeh


        Comment

        • Rolf Magnus

          #5
          Re: detecting if no exception was thrown

          Aryeh M. Friedman wrote:
          [color=blue][color=green]
          >> Or if you know that test_pass(0) doesn't throw x:
          >>
          >> try {
          >> expr;
          >> test_pass(0);
          >> } catch(x) {
          >> test_fail(0);
          >> }[/color]
          >
          > Actually the test only passes if it throws x[/color]

          Then just exchange test_pass and test_fail.

          Comment

          Working...