web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
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

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    239,684 Most Valuable Professional on at

    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.

  • Suggested answer
    Erik HB Profile Picture
    205 on at

    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.

  • Georgiev Profile Picture
    45 on at

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

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 21

#2
dekion Profile Picture

dekion 4

#2
Virginia99 Profile Picture

Virginia99 4

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans