Menu

Not executing CPP_FAIL inside a catch block.

Help
2003-10-08
2003-10-09
  • Geoff Drake

    Geoff Drake - 2003-10-08

    Hi
    Here is an example of my problem.  I am running thhis inside Eclispe 2.1.1 using the CPPUnit Plugin.  If I comment ou the call o throwException() I get the expected CPP_FAIL.  If I catch the exception it does not execute the CPP_FAIL, but uses a generic catch.  Is this a bug, or am I doing something wrong?

    #include <iostream>

    #include "cppunit/Test.h"
    #include "cppunit/extensions/HelperMacros.h"
    using namespace std;

    class SimpleException
    {
        private:
            char*message;
           
        public:
            SimpleException(char *msg)
            {
                message = msg;
            }
           
            char *getMessage()
            {
                return message;
            }
    };

    class CrashTest: public CppUnit::TestFixture {

    private:

            void throwException()
            {
                throw new SimpleException("Hey an exception");
            }
           
    public:

        void setUp()
        {
        }
       
        void tearDown()
        {
        }
       
        void testOne ()
        {
            try
            {
                // If throwException is commented out I get "Should have thrown..."
                // If left in I get "Message:caught unknown exception" not ex.getMessage()
                throwException();
                CPPUNIT_FAIL("Should have thrown an Exception before here");
            }
            catch (SimpleException ex)
            {
                CPPUNIT_FAIL(ex.getMessage());
            }
        }
       
       
        CPPUNIT_TEST_SUITE(CrashTest);
        CPPUNIT_TEST(testOne);
        CPPUNIT_TEST_SUITE_END();
       
       
    };
    CPPUNIT_TEST_SUITE_REGISTRATION(CrashTest);

     
    • Hisakuni Kitahara

      You throw an exception as pointer type of SimpleException,
      so you shoud code catch block as follow

      catch (SimpleException *ex)
      {
           CPPUNIT_FAIL(ex->getMessage());
      }

      Or throw

      throw SimpleException("Hey an exception");

      Rigards,
      Hisakuni

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.