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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Exception Handling in X++

(0) ShareShare
ReportReport
Posted on by 390

Hey Team,

I want the system generated error message when an exception has occurred. Please can you let me know how can I get this using x++ code.

I want to store the errors in a table.

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    You can hook in Classes\Info\add to get this info.

  • Mingzhou Deng Profile Picture
    on at

    Hi,

    do you mean you want to catch the exception in your own code and throw as an error? please share more details on your requirement. X++ exception/error handling is pretty robust now.

  • skd Profile Picture
    390 on at

    hey Mingzhou Deng,

    Yes, actually I want to catch the exception and show it to the user on the UI as my code runs as a batch job and therefore if any error occurs I want to catch the exception and show it to the user on the UI

  • skd Profile Picture
    390 on at

    Hey Nikolaos Mäenpää,

    Can you please elaborate a little more on this, how can I do this?

  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    Aha, so this is duplicate of your other thread, Show Batch Job Error on the UI, the difference is that you didn't explain your actual problem here. It seems we should abandon this thread and continue the original one.

  • skd Profile Picture
    390 on at

    Hey Martin Dráb ,

    Yes, we can continue there. I initially planned to get the batch job logs but it seems to be difficult so later I thought of using the exception in catch block hence I created a new thread.

  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    If you have a different intent in this thread, explain it. We can't read your mind - we depends on what you wite down, which wasn't much.

    We can't help you if you didn't do your part and explain your problem in sufficient detail.

  • skd Profile Picture
    390 on at

    Okay, I will explain in detail what I need. I have a batch job which runs to create associates(or users simply), many a times the batch job fails due to some reason. I have a logTable which stores details of the batch job. If the associate is created successfully it stores details as "Associate created successfully" but if there is an error in the batch job I want to show that as well on UI and store it in the logTable.So I have a try catch block in my batch job so if the batch job fails I want to catch the system generated Exception from the batch job and store it in my log table.

    My catch block:

     AssociateTable associate;
     catch(Exception::Error)
     
                {
                    if (associate.RecId)
                    {
                        ttsbegin;
                       
                        Error(strFmt("@HRM:AssociateCreationErrorStatus"));
                        associate.AssociateStatus = AssociateStatus::Failed;
                        associate.update();
    
                        
                        
                        ttscommit;
                    }
                }

    My log table

    ExceptionError.png

    Example of the exception I want to show:

    //just an example I copied from the docs:

    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentOutOfRangeException: StartIndex cannot be less than zero. Parameter name: startIndex at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) at System.String.Substring(Int32 startIndex) at ClrBridgeImpl.InvokeClrInstanceMethod(ClrBridgeImpl* , ObjectWrapper* objectWrapper, Char* pszMethodName, Int32 argsLength, ObjectWrapper** arguments, Boolean* argsAreByRef, Boolean* isException) **********/ }

  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    That's definitely the same topic as in your other thread. As already discussed there, you have this information in existing batch logs. I would avoid duplicating it, but if you want, look at how it's done in the standard application.

    If you have more question on this topic, ask them in Show Batch Job Error on the UI.

  • skd Profile Picture
    390 on at

    Hey Martin Dráb,

    I found a blog which I feel is similar to my issue I have added that blog in my previous thread could you pls help with that blog. Will that blog resolve my issue?

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 456 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans