web
You’re offline. This is a read only version of the page.
close
Skip to main content
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
    236,279 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.

  • 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.

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

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#2
Guy Terry Profile Picture

Guy Terry 2 Moderator

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans