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 228,493 Most Valuable Professional 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.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,493 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans