locked
which part of the code do I need to place in the catch block to handle the exception? RRS feed

  • Question

  • Hello All,

    From the below function, when executing waitParam.SetValue(true); it is throwing an exception from ~TypeValue() destructor.

    To catch the exception, I have used try/catch but it is not handling the exception. Which part of the code do I need to place in the catch block to handle the exception?


    I have fixed the issue by changing

    if (mStringVal)

    with

    if (mKind == pkString && mStringVal != nullptr)

    in ~TypeValue() destructor.

    But wanted to know which part of the code do I need to place in the catch block to handle the exception?

    ~TypeValue()
    {
    if (mKind == pkString && mStringVal != nullptr)
    {
    delete mStringVal;
    mStringVal = nullptr;
    }
    };

    Below is the complete code:

    int PCPEAutomation::Execute()
    {
    std::wstring strFilePath = L"C:\PCPEAutomationDev\src\AutomationFiles";
    DWORD dwResult = ERROR_SUCCESS;
    try
    {
    PCPECLIAutomation pcpeCLIAutomation;

    // Set working dir param
    Parameter workingDirParam(PARAMETER_WORKING_DIR);
    workingDirParam.SetValue(strFilePath);
    pcpeCLIAutomation.SetParameter(workingDirParam);

    // Set wait flag param
    Parameter waitParam(PARAMETER_WAIT_FOR_COMPLETE);
    waitParam.SetValue(true);
    pcpeCLIAutomation.SetParameter(waitParam);

    //Execute the CLI operation
    m_nStatus = pcpeCLIAutomation.Execute();
    }
    catch (...)
    {
    dwResult = ERROR_UNHANDLED_EXCEPTION;
    }
    return m_nStatus;
    }

    Parameter.h

    #include "TypeValue.h"

    class Parameter
    {
    private:
    std::wstring mID;
    TypeValue mValue;
    public:
    Parameter()
    {
    }
    Parameter(std::wstring id) :
    mID(id)
    {
    }
    ~Parameter()
    {

    }
    bool SetValue(const PropertyValue& value)
    {
    mValue = value;
    return true;
    }
    bool GetValue(PropertyValue& value) const
    {
    value = mValue;
    return true;
    }
    std::wstring GetID() const
    {
    return mID;
    }
    };

    TypeValue.h

    enum TypeValueKind
    {
    pkEmpty = 0,
    pkBool = 1,
    pkInt = 2,
    pkString = 3
    };

    class TypeValue
    {
    private:
    union
    {
    bool mBoolVal;
    int mIntVal;
    std::wstring *mStringVal;
    };

    TypeValueKind mKind;

    public:
    typedef std::vector<TypeValue*> ArrayVal;

    TypeValue() : mKind(pkEmpty)
    {
    mBoolVal = false;
    mIntVal = 0;
    mStringVal = nullptr;
    };

    ~TypeValue()
    {
    if (mStringVal)
    {
    delete mStringVal;
    mStringVal = nullptr;
    }
    };

    TypeValue(bool val) : mKind(pkBool)
    {
    mBoolVal = val;
    }

    TypeValue(int val) : mKind(pkInt)
    {
    mIntVal = val;
    }

    TypeValue(std::wstring &val) : mKind(pkString)
    {
    mStringVal = new std::wstring();
    *mStringVal = val;
    }
    };

    Could anyone please help me on this?

    Friday, November 1, 2019 4:19 PM

Answers

  • ~TypeVal destructor doesn't throw a C++ exception - it exhibits undefined behavior, by way of accessing an inactive member of a union. In other words, it's buggy and the program is crashing. The way to deal with it is to fix the bug. And you even already know how.

    Igor Tandetnik

    Friday, November 1, 2019 5:43 PM

All replies

  • ~TypeVal destructor doesn't throw a C++ exception - it exhibits undefined behavior, by way of accessing an inactive member of a union. In other words, it's buggy and the program is crashing. The way to deal with it is to fix the bug. And you even already know how.

    Igor Tandetnik

    Friday, November 1, 2019 5:43 PM
  • Hello,

    Thank you for posting here.

    Is this issue still exist? If you resolved this issue, could you please mark the helpful reply as answer. we also welcome you to share your own solution and mark it as answer, this will help other community members who face the similar problems.

    Best Regards,

    Suarez Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 4, 2019 7:16 AM