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, ...
Answered

X++ Catch does not catch the error

(0) ShareShare
ReportReport
Posted on by 238

Hey,

i am kind a new on development D365 Fo i faced problem about try catch statement which is not exception not catched by catch.

When i trying to insert my customProd method throws error. I am expecting this insert error should be catched by statement .

When i trying to debug i can see  "inventory dimensions site must be consistent within the lot" error on infolog.

public void customProd(List _myList)
{
    MyClass myClass;
    literator = new ListIterator(_myList.parmTamamlandi());
    while(literator.more())
    {
        myClass = literator.value();
        ttsbegin;
        if(!this.prodStart(myClass))
        {
            ttsabort;
            literator.next();
            continue;
        }
        if(!this.finished(myClass))
        {
            ttsabrot;
            literator.next();
            continue;
        }
        ttscommit;
    }
}

public boolean finished(MyClass _myClass)
{
    try
    {
        // setting prodjournal table
        
        // setting prodjournalprod table
        
        prodJournalProd.insert(); 
        
    }
    catch
    {
        // i want to do something here 
    }
}

otherhand when i remove tts statement from customProd method and put them into finished method its working fine.

Is there anything i am missing ? what should i do for catch the error on first statement.

public void customProd(List _myList)
{
    MyClass myClass;
    literator = new ListIterator(_myList.parmTamamlandi());
    while(literator.more())
    {
        myClass = literator.value();
        //ttsbegin;
        if(!this.prodStart(myClass))
        {
            //ttsabort;
            literator.next();
            continue;
        }
        if(!this.finished(myClass))
        {
            //ttsabrot;
            literator.next();
            continue;
        }
        //ttscommit;
    }
}

public boolean finished(MyClass _myClass)
{
    try
    {
        ttsbegin;
        // setting prodjournal table
        
        // setting prodjournalprod table
        
        prodJournalProd.insert(); 
        ttscommit;
        
    }
    catch
    {
        ttsabort;
        // i want to do something here 
    }
}

Edt: second statement added. and this catch working totaly fine.

I have the same question (0)
  • Suggested answer
    YY Lim Profile Picture
    960 on at

    May I know what are you trying to do? Please do not use ttsabort in X development if possible because it would cause unbalance tts transaction.

    Please refer this docs.microsoft.com/.../x-standards-ttsbegin-and-ttscommit

    Not sure your intention but change to something like this.

    public void customProd(List _myList)
    {
        MyClass myClass;
        literator = new ListIterator(_myList.parmTamamlandi());
        while(literator.more())
        {
            myClass = literator.value();
            //ttsbegin;
            if(!this.prodStart(myClass))
            {
                //ttsabort;
                //do not use ttsabort, throw error, it won't continue the subsequent statement anyway.
                throw error("Testing error 1");
                literator.next();
                continue;
            }
            if(!this.finished(myClass))
            {
                //ttsabrot;
                throw error("Testing error 2");
                literator.next();
                continue;
            }
            //ttscommit;
        }
    }
    
    public boolean finished(MyClass _myClass)
    {
        List testingList;
        
        try
        {
            ttsbegin;
            // setting prodjournal table
            this.customProd(testingList);
            // setting prodjournalprod table
            
            prodJournalProd.insert(); 
            ttscommit;
            
        }
        catch
        {
            //ttsabort; You dont need this ttsabort here
            // i want to do something here 
        }
    }

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

    Your assumption is incorrect. The 'catch' statement won't have any effect, because exceptions aren't caught inside transactions (most types of exceptions, to be more precise).

  • Ferhat.S Profile Picture
    238 on at

    Hey, to explain my goal is gather prodfinished and prodstart under one transaction like if there is a some problem on prodfinished i want to call back my prodstart. I achive it similar structer on ax 2009.

    first pair of code which is working as i want like 

    public void customProd(List _myList)
    {
        MyClass myClass;
        literator = new ListIterator(_myList.parmTamamlandi());
        while(literator.more())
        {
            myClass = literator.value();
            if(!this.prodStart(myClass))
            {
                literator.next();
                continue;
            }
            if(!this.finished(myClass))
            {
                literator.next();
                continue;
            }
        }
    }
    
    public boolean finished(MyClass _myClass)
    {
        try
        {
            ttsbegin;
            // setting prodjournal table
            
            // setting prodjournalprod table
            
            prodJournalProd.insert(); 
            // if there is something wrong ie wrong dimension etc bellow catch statement catching error
            ttscommit;
            
        }
        catch
        {
            ttsabort;
            // i want to do something here 
        }
    }

    But in this code i will not able to call back prodstart

    so i changed transactions like

    public void customProd(List _myList)
    {
        MyClass myClass;
        literator = new ListIterator(_myList.parmTamamlandi());
        while(literator.more())
        {
            myClass = literator.value();
            ttsbegin;
            if(!this.prodStart(myClass))
            {
                ttsabort;
                literator.next();
                continue;
            }
            if(!this.finished(myClass))
            {
                ttsabort;
                literator.next();
                continue;
            }
            ttscommit;
        }
    }
    
    public boolean finished(MyClass _myClass)
    {
        try
        {
            
            // setting prodjournal table
            
            // setting prodjournalprod table
            
            prodJournalProd.insert(); 
            // if there is something wrong ie wrong dimension etc bellow catch statement wont catching error
           
            
        }
        catch
        {
            // i want to do something here 
        }
    }

    inside catch in my reportfinsihed wont catching the error and throws it to main method i wont do that due to my list be going "n" times and if there is a mistake on first i will not able to do rest.

    At catch statements i will prepare return message and notfiy people with whats going wrong at that message.

    Hope could explained my self.

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

    As we said, you can't catch the exception inside a transaction, therefore your code can't work. Also, calling ttsabort is catch makes no sense, because throwing an exception aborts the transaction. Both worked exactly the same in AX 2009.

    Please tell us more about your requirements, so we can help you to desing something that can work. What exactly do you want to do in a case of failure? If you want to simple rolled back all inserts done before the error occurred, just the whole thing to a single transaction and the rollback will be done automatically. If you want something else, please explain what it is.

  • Ferhat.S Profile Picture
    238 on at

    Hey Martin,

    Yes you right catch statement abort transactions.

    Otherhand i can say yeah i want to simple roll back on my previous inserts on error occurs. But i dont want to write them on single method. Due to i want to use those methods later on.

    To explain my project its a class which is working with integrations to put prodstart protfinished and prodbom into axapta. So i want to achive them with seperate methods with in one transaction blog. But seems like catch statement will not catch the error where i want.

  • Suggested answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    You need to put your transaction to customProd() and let it cover all insers. When an exceptions gets thrown, all changes done in the transaction will be rolled back, even if they were done by methods called from customProd() and not just customProd() itself.

    You also can catch errors there and "prepare return message and notfiy people with whats going wrong at that message", but you need the transaction inside try, not try/catch inside a transaction. For example:

    public void customProd(List _myList)
    {
    	try
    	{
    		ttsBegin;
    		
    		ListEnumerator enumerator = _myList.getEnumerator();
    		
    		while (enumerator.moveNext())
    		{
    			MyClass myClass = enumerator.current();
    			
    			... do your stuff ...
    		}
    		
    		ttsCommit;
    	}
    	catch
    	{
    		... error handling ...
    	}
    }

  • Ferhat.S Profile Picture
    238 on at

    Yes, this will be handle error. But could you tell me what will i do rest of list when error occured first of message. I mean i dont know how to return while statement after catch statement.

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

    I'm sorry, but I have no idea what you're saying. Please elaborate.

  • Ferhat.S Profile Picture
    238 on at

    Okey sorry.

    Lets assume my list contains 10 elements. I executed first element and when i trying to execute second one it throws error and my catch statement catched the error. But i want to return my code for rest of 8 elements. Thats what i was asking.I hope could explained.

    I might be found a solution while writing. is that posible remove corrupted message from list and using retry and execute rest of list.

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

    What is "to return my code"?

    Do I understand correctly that you want to rollback everything inserted so far and continue with the rest of the list? For example, you ignore those two and continue with the eight. Fifth of of them failed, so you rollback these five too and continue with the remaining three?

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
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans