Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Handle exception in try catch block

(0) ShareShare
ReportReport
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.

*This post is locked for comments

  • Georgiev Profile Picture
    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
    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
    234,035 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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics

Product updates

Dynamics 365 release plans