Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Unanswered

Question about error trap

(0) ShareShare
ReportReport
Posted on by 592

Hi folks,

I got some development question, especially to get the correct error message that I would like to rectify.

I'm creating a class that have this flow in that same one class: 

internal final class ThisClass()
{

    static void main(Args   _args)
    {
    
        try
        {
            ThisClass   thisClass = new ThisClass();
            if (thisClass.transfer())
            {
                
            }
        }
        catch (Exception::Error)
        {
            error("Critical error has been thrown and the process is stopped.");
        }
    }
        
    public boolean transfer()
    {
        boolean  success = false;
        try
        {
            if (this.validateData())
            {
                ttsbegin;
                
                if ( this.createTransData())
                    success = true;
                
                
            
                ttscommit;
            }
        }
        catch (Exception::Error)
        {
            ttsabort;
            container       infoData = infolog.infologData();
            Str1260         errorTxt;
            for (int i=1;i<= conLen(infoData);i  )
            {
                container  internalInfoData = conPeek(infoData,i);
    
                if (conPeek(internalInfoData,1) == Exception::Error
                || conPeek(internalInfoData,1) == Exception::Warning)
                {
                    if (errorTxt == "")
                    {
                        errorTxt = conPeek(internalInfoData,2);
                    }
                    else
                    {
                        errorTxt = errorTxt   ";"   conPeek(internalInfoData,2);
                    }
                }
            }
        }
        
        return success;
    }


    public boolean validateData()
    {
        if (myTable.field == "")
        {
            success = false;
            throw error(strFmt("@SYS136411",
                    fieldId2PName(tableNum(MyTable), fieldNum(MyTable, field))));
        }
        return success;
    }


}

To make more simple explanation, the path should be : Main() -> transfer() -> validateData() 

My question is when in that validation method which will check my field, if it is empty then it will throwError then return false. Then as per my understanding it will going to Catch in the caller method, which is also correct at this point, it is going to that catch(Exception:Error)

However If I look at the debug, I wonder why in that infologData() has already contain several message which I thought it will retrieve after this 1st throw error, like below:

pastedimage1679909686004v1.png

As we can see, the infolog already contain 5 rows container, which no 3 is the message from Main(), also in its 1st row container, it is a system message saying cannot edit record which I believe because there is some statement of "update recordset" which I put after it is ran that method validateData(), which I thought since it is failed validation, it will not even go to update_recordset (but this is my thought only)

I'm wondering whether this is the correct way of error trap, and secondly, if I want to get the last row of the infolog container only which is the one I need to tell my user in some Log table, where is the correct place to insert the message and how to retrieve only the last (no 4) infolog container.

Thanks,

  • Martin Dráb Profile Picture
    Martin Dráb 230,934 Most Valuable Professional on at
    RE: Question about error trap

    You're still claiming that 'throw error' returns false; but that's a nonsense. Neither the statement nor the method where you call it returns true or false. Instead, an exception is thrown. Then you're right in expecting that when an exception is thrown, it may be caught by a catch statement. It wouldn't be true if a method really returned false. You seem to be confirming that the exception is indeed thrown and caught, therefore it all works correctly (although not according to your description). If that's true, then there is problem with throw/catch and this whole discussion is irrelevant to your actual problem.

    You code reads everything currently in infolog (which may include old unrelated messages), not just what was added validateData(). You could fix that by checking the infolog line number (infologLine()) before running your process and reading messages just from this line.

  • Voltes Profile Picture
    Voltes 592 on at
    RE: Question about error trap

    Ok, I think I got everyone lost at my explanation of this word ""when I hit error in my validation, then it will hit throwError".

    Let me try to rephrase it (I changed the original and copied back here as well) -->

    My issue is when in that validation method which will check my field, if it is empty then it will throwError then return false. Then as per my understanding it will going to Catch in the caller method, which is also correct at this point, it is going to that catch(Exception:Error)

    Hope my assumption on where it is got lost is correct. So, it is not about my code has error, but when the program run, and as the flow goes, it will goes to that method validateData(), and if it is true that my field is empty, then it will throw Error.

    And the question is why the infolog contain that 5 rows with all those messages ?

    My intention is to get only the message from the validateData(), and I'm planning to insert this specific message into some log table. So I want to know where is the best place to put that insert statement.

    Thanks,

  • Martin Dráb Profile Picture
    Martin Dráb 230,934 Most Valuable Professional on at
    RE: Question about error trap

    I'm sorry, but I can't address your problem, because I don't understand your description of the problem. Consider explaining it once more.

  • Voltes Profile Picture
    Voltes 592 on at
    RE: Question about error trap

    Hi,

    Yes, that's why I'm asking here to know, what or how to make sense / the correct way. Which one I should correct it ?

    Thanks.

  • Martin Dráb Profile Picture
    Martin Dráb 230,934 Most Valuable Professional on at
    RE: Question about error trap

    If you mean the throw statement when talking about throwError, the sentence still doesn't make sense. The throw statement doesn't return false - it throws an exception.

  • Voltes Profile Picture
    Voltes 592 on at
    RE: Question about error trap

    Hi Martin,

    There is "throw error" in method validateData(). When checking the field in my table empty or not, when it is empty, execute Throw error.

    Thanks,

  • Martin Dráb Profile Picture
    Martin Dráb 230,934 Most Valuable Professional on at
    RE: Question about error trap

    I got lost already at the beginning of your problem description. You said "when I hit error in my validation, then it will hit throwError which is expected then return false", but I see no throwError method in code that you shared with us.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,111 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,934 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans