Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Handle exception in try catch block

Posted on by 45

I have a switch case scenario where I know beforehand that there is a high level of opportunity that the user might not set the the rules with the right categories. it's totally ok for me as soon as i can pop up a dialog showing instructions what to do in order to run the program. However i am missing something and here is my question.

When i test a scenario as per above the cursor goes through the default case, and pops up the error. How can i make it goes into my catch block?

try
{

    switch (userInput)
    {
        case 1:
        case 2:
        case 3:
            if (condition)

            {
               some code here
            }
            else
            {
                throw error();
            }
            break;
        case 4:
            postingType = smth;
            break;
        default:
            throw Global::error("here blows the error and does not move on to the catch");
    }
}
catch(exception::Error) { info("test"); }

Your help is much appreciated!

Thank you in advance and have a nice evening.

H.

  • Georgiev Profile Picture
    Georgiev 45 on at
    RE: Handle exception in try catch block

    Thanks for your advice, helped me get through this! E

  • Suggested answer
    Erik HB Profile Picture
    Erik HB 205 on at
    RE: Handle exception in try catch block

    Another solution would be to not use exceptions for this type of error handling but a boolean flag instead, alternatively write error messages into a string variable and then at the end check whether the string is empty or not.

    Handling errors that way works even inside transactions and it leaves exceptions for what would be unexpected situations which do require special handling.

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 224,528 Super User on at
    RE: Handle exception in try catch block

    Check if your code doesn't execute inside a transaction (you can see a TTS level in debugger). If it's the case, your catch statement is ignored because the program execution continues only after the end of the transaction. You would to add exception handling outside the transaction; making transaction short and well-defined helps in reducing these problems.

Helpful resources

Quick Links

Can you answer this forum question?

You could make someone's day!

Community Newsletter - May 2024

Kudos to our community stars!

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 283,632 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 224,528 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,146

Featured topics

Product updates

Dynamics 365 release plans