Handling exceptions for winrt::hresult

Noah Aas 925 Reputation points
2025-03-27T16:49:55.12+00:00

Hello,

Dialog -- Object -- MyTest

If I have an exception I need to go inside there ########

void CMFCCopyDataDlg::RequestPanelIn(CString data)
{
try
{
CurrentPanel.SetPanelInfos(data);
}
catch (exception ex)
{

}
catch (char* errorMessage)
{
// Inside ######## triggered by function below
CString errorfrom(errorMessage);


// -------------------------------------------------------

Not working, why?

void MyTest::SetPanelInfos(CString data)
{
if (JsonObject::TryParse(jsonstr, obj))
{
try
{
MessageId = obj.GetNamedString(L"MessageId").c_str();
Station = obj.GetNamedString(L"Station").c_str();

Program = obj.GetNamedString(L"Program").c_str();
Order = obj.GetNamedString(L"Order").c_str();
Referencecode = obj.GetNamedString(L"Re ferencecode").c_str();

OrderQuantity = obj.GetNamedNumber(L"OrderQuantity");
OrderQuantityCurrent = obj.GetNamedNumber(L"OrderQuantityCurrent");
OrderQuantityGood = obj.GetNamedNumber(L"OrderQuantityGood");
OrderQuantityBad = obj.GetNamedNumber(L"OrderQuantityBad");
}
catch (winrt::hresult_error const& ex)
{
winrt::hresult hr = ex.code(); // HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND).
winrt::hstring message = ex.message(); // The system cannot find the file specified.


hstring jsonstrError = winrt::to_hstring(message);

std::string tempStr(message.begin(), message.end());
CString sError = tempStr.c_str();
sError.Format("Error;%s", sError);

string error = "Error;" + tempStr;
throw error;

If I write this, works (not as string variable)

catch (winrt::hresult_error const& ex)
{
winrt::hresult hr = ex.code(); // HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND).
winrt::hstring message = ex.message(); // The system cannot find the file specified.
throw "Error- TEST;

Then I can get in there.

But I need the whole message. The best with Error number and Error message.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,904 questions
{count} votes

Accepted answer
  1. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2025-03-28T02:59:00.5+00:00

    Hi,

    winrt::hresult_error

     catch (winrt::hresult_error const& ex)
        {
            winrt::hresult hr = ex.code (); // HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND).
            winrt::hstring message = ex.message (); // The system cannot find the file specified.
            throw winrt::hresult_error (hr, message);
        }
    

    As Darran Rowe said, there are HasKey and

    GetNamedString(String, String)

    Gets the String value with the specified name, or the provided default value if no such named value is found.

    So throwing an exception is not needed.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.