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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Try and Catch - Exception handling. Error is not getting caught in Catch part

(4) ShareShare
ReportReport
Posted on by 55
Hi,
 
I am able to create product receipt just fine for inbound integration using the blog listed in this chat. 
Problem is code never hits to the catch block when testing for negative scenarios. I am calling createProductReceipt() in a batch class.
xyxProductReceiptImport - is rip off from the code in blog below. I even wrote try and catch in this method where the error is being issued from purchformletter public server void processProductReceipt()
 
How to handle this exception or catch it in catch block?
 
private void createProductReceipt()
{
       
                try
                {
                    
                    ttsbegin;
                    xyxProductReceiptImport productReceiptImport = xyxProductReceiptImport::newFromParameters(purchTable,
                                                                                                              packingSlipId,
                                                                                                              packingSlipDate);
                    productReceiptImport.run(purchLine, batchId, qtys);
 
                    ttsCommit;
                }
                catch (Exception::Error)
                {
                    ttsAbort;
                    ttsbegin;
                    queueTable.selectForUpdate(true);
                    queueTable.ErrorMesssage += "\n " + errorNotes;
                    queueTable.Status = RequestStatus::Error;
                    queueTable.update();
                    ttsCommit;
                }
                catch (Exception::CLRError)
                {
                    ttsAbort;
                   ttsbegin;
                    System.Exception interopException = CLRInterop::getLastException();
                    info(strFmt(interopException.ToString()));
 
                    queueTable.selectForUpdate(true);
                    queueTable.ErrorMesssage += "\n " + errorNotes;
                    queueTable.Status = RequestStatus::Error;
                    queueTable.update();
                    ttscommit;
                }
}

 

public server void processProductReceipt()

{

 

     try

     {

         purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);

         purchFormLetter.transDate(DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()));

         purchFormLetter.proforma(false);

         purchFormLetter.specQty(PurchUpdate::Recorded);

         purchFormLetter.purchTable(purchTable);

         purchFormLetter.parmParmTableNum(purchParmTable.ParmId);

         purchFormLetter.parmId(purchParmTable.ParmId);

         purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());

         purchFormLetter.run();

         boolean ret = purchFormLetter.validate();

     }

 

     catch (Exception::Error)

     {

         Info("");

     }


}

 
 
Categories:
I have the same question (0)
  • Martin Dráb Profile Picture
    238,734 Most Valuable Professional on at
    Isn't createProductReceipt() executed inside a transaction? If so, you can't catch exception there. You can verify TtsLevel in debugger.
     
    By the way, there is no reason to use ttsAbort in the catch block. If an exception occurs, the transaction gets aborted automatically.
     
    Also, there is a better way of catching CLR exceptions than using catch (Exception::CLRError) and CLRInterop::getLastException():
    System.Exception ex;
    
    try
    {...}
    catch (ex)
    {...}
     
  • Ethan Hunt Profile Picture
    55 on at
    Thank you Martin for your reply! 
     
    private void createProductReceipt()
    This method is in the run method of the batch. There is no  ttsbegin or ttscommit. 
     
     
    Try
    {
    ttsbegin;
     post product receipt
    ttscommits;
    }
     
    Catch
    {
      catch any error
    }
    Yes, i did verify the ttslevel as you have mentioned in another thread. I see ttslevel is zero by the time it exits from purchformletter.run();
     
     
    When there is an error while posting product receipt, it must be handled for me. So that it can be rerun in another time after fixing the data or whatever.
     
    edit:
    I revised the code like below. removing unnecessary helper class and moved all the code to batch. it still fails to go to the catch part.
  • Ethan Hunt Profile Picture
    55 on at
    class xyxProductReceiptImportBatchCopy extends RunBaseBatch
    {
     
        PurchFormLetterParmData          purchFormLetterParmData;
        PurchParmTable                   purchParmTable;
        PurchParmLine                    purchParmLine;
        PurchParmUpdate                  purchParmUpdate;
        PurchTable                       purchTable;
        PurchFormLetter                  purchFormLetter;
        TransDate                        packingSlipDate;
        PackingSlipId                    packingSlipId;
     
        public void run()
        {
            xyxProductReceiptImport xyxProductReceiptImport = new xyxProductReceiptImport();
     
            xyxProductReceiptImportQueueTable queueTable;
     
            boolean ret;
            while select * from queueTable
                where (queueTable.Status == xyxRequestStatus::Error
                    || queueTable.Status == xyxRequestStatus::Ready)
            {
                infolog.clear();
                Notes errornotes = "";
     
                changecompany(queueTable.xyxDataAreaID)
                {
                    ret = this.validateData(queueTable.xyxDataAreaID, queueTable.PurchId, queueTable.InventBatchID,
                                queueTable.PackingSlipID, queueTable.Qty, queueTable.ItemId);
                    if(ret)
                    {
                        try
                        {
                            PurchLine       purchLine;
                            PurchFormLetter purchFormLetterLocal;
                            Qty             qtys;
                            purchTable              = PurchTable::find(queueTable.PurchId);
                            packingSlipDate         = DateTimeUtil::date(DateTimeUtil::utcNow());
                            packingSlipId           = queueTable.PackingSlipID;
                            InventBatchId           batchId;
     
                            select firstonly forupdate purchLine
                                    where purchLine.PurchId == purchTable.PurchId
                                        && purchLine.ItemId == queueTable.ItemId;
     
                            InventBatch inventBatchOld, inventbatchUpdated;
                            select firstonly inventBatchOld
                                    where inventBatchOld.inventBatchId == queueTable.InventBatchID
                                           && inventBatchOld.itemId == purchLine.ItemId;
     
                            if(!inventBatchOld)
                            {
                                inventbatchUpdated.itemId          = purchLine.ItemId;
                                inventbatchUpdated.inventBatchId   = queueTable.InventBatchID;
                                inventbatchUpdated.prodDate        = queueTable.ManufactureDate;
                                inventbatchUpdated.expDate         = queueTable.ExpirationDate;
                                inventbatchUpdated = InventBatch::findOrCreate(batchId, purchLine.ItemId);
                                batchId = inventbatchUpdated.inventBatchId;
                            }
     
                            else
                            {
                                batchId = inventBatchOld.inventBatchId;
                            }
     
                            InventDim inventDim     = purchLine.inventDim();
                            inventDim.inventBatchId = batchId;
                            inventDim               = inventDim::findorcreate(inventDim);
     
                            if(purchLine.inventDim().inventDimId != inventDim.inventDimId)
                            {
                                ttsbegin;
                                purchLine.InventDimId = inventDim.inventDimId;
                                purchLine.update();
                                purchFormLetterLocal = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);
                                purchFormLetterLocal.update(purchTable, purchTable.PurchId, systemDateGet());
                                ttscommit;
                            }
     
                            this.insertParmTableData();
                            this.insertParmLineData(purchLine, batchId, qtys);
                            this.processProductReceipt();
                        }
     
                        catch (Exception::Error)
                        {
                            queueTable.selectForUpdate(true);
                            queueTable.ErrorMesssage    = queueTable.ErrorMesssage + "\n " + errornotes;
                            queueTable.Status           = xyxRequestStatus::Error;
                            //queueTable.update();
                        }
     
                        catch (Exception::CLRError)
                        {
                            System.Exception interopException = CLRInterop::getLastException();
                            Info(strFmt(interopException.ToString()));
                            queueTable.selectForUpdate(true);
                            queueTable.ErrorMesssage    = queueTable.ErrorMesssage + "\n " + errornotes;
                            queueTable.Status           = xyxRequestStatus::Error;
                        }
                    }
                    else
                    {
                        int i;
                        for(i = 1; i<infolog.line(); i++)
                        {
                            errornotes += infolog.text(i);
                        }
                        ttsbegin;
                        queueTable.selectForUpdate(true);
                        queueTable.ErrorMesssage    = queueTable.ErrorMesssage + "\n " + errornotes;
                        queueTable.update();
                        ttscommit;
                    }
                }
            }
        private void insertParmTableData()
        {
            purchFormLetterParmData = PurchFormletterParmData::newData(DocumentStatus::PackingSlip,
                                         VersioningUpdateType::Initial);
            purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
            purchFormLetterParmData.createData(false);
            purchParmUpdate = purchFormLetterParmData.parmParmUpdate();
     
            purchParmTable.clear();
            purchParmTable.TransDate                = SystemDateGet();
            purchParmTable.Ordering                 = DocumentStatus::PackingSlip;
            purchParmTable.ParmJobStatus            = ParmJobStatus::Waiting;
            purchParmTable.Num                      = packingSlipId;
            purchParmTable.PurchId                  = purchTable.PurchId;
            purchParmTable.PurchName                = purchTable.PurchName;
            purchParmTable.DeliveryName             = purchTable.DeliveryName;
            purchParmTable.DeliveryPostalAddress    = purchTable.DeliveryPostalAddress;
            purchParmTable.OrderAccount             = purchTable.OrderAccount;
            purchParmTable.CurrencyCode             = purchTable.CurrencyCode;
            purchParmTable.InvoiceAccount           = purchTable.InvoiceAccount;
            purchParmTable.ParmId                   = purchParmUpdate.ParmId;
            purchParmTable.insert();
        }
     
        public server void processProductReceipt()
        {
            try
            {
                purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);
                purchFormLetter.transDate(DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()));
                purchFormLetter.proforma(false);
                purchFormLetter.specQty(PurchUpdate::Recorded);
                purchFormLetter.purchTable(purchTable);
                purchFormLetter.parmParmTableNum(purchParmTable.ParmId);
                purchFormLetter.parmId(purchParmTable.ParmId);
                purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
                purchFormLetter.run();
            }
     
            catch (Exception::Error)
            {
                Info("");
            }
     
            catch (Exception::CLRError)
            {
                Info("");
            }
        }
     
        private void insertParmLineData(PurchLine _purchLine, InventBatchId _inventBatchId, Qty _qtys)
        {
            PurchLine purchLine;
            InventDim inventDim;
            int       i;
            purchParmLine.InitFromPurchLine(_purchLine);
            purchParmLine.InventDimId   = _purchLine.inventDim().inventDimId;
            purchParmLine.ReceiveNow    = _qtys;
            purchParmLine.ParmId        = purchParmTable.ParmId;
            purchParmLine.TableRefId    = purchParmTable.TableRefId;
            purchParmLine.setQty(DocumentStatus::PackingSlip, false, true);
            purchParmLine.setLineAmount();
            purchParmLine.insert();
            this.registerInventory(purchParmLine, inventDim);
        }
     
        private void registerInventory(PurchParmLine _purchParmline, InventDim _inventDim)
        {
            InventTransWMS_Register inventTransWMS_register;
            TmpInventTransWMS       tmpInventTransWMS;
            InventDim               inventBatchCheck;
            InventTrans             inventTranslocal;
            InventDim               inventDimlocal;
     
            inventTranslocal                = InventTrans::findTransId(_purchParmline.InventTransId, true);
     
            inventDimlocal                  = inventTranslocal.inventDim();
            inventDimlocal.inventBatchId    = _inventDim.inventBatchId;
            inventDimlocal.InventLocationId = _inventDim.InventLocationId;
            inventDimlocal.InventSiteId     = _inventDim.inventSiteId;
            inventDimlocal.wMSLocationId    = _inventDim.wMSLocationId;
            inventDimlocal                  = InventDim::findOrCreate(inventDimlocal);
            inventTransWMS_register         = inventTransWMS_register::newStandard(tmpInventTransWMS);
            inventTranslocal.inventDimId    = inventDimlocal.inventDimId;
            tmpInventTransWMS.clear();
            tmpInventTransWMS.initFromInventTrans(inventTranslocal);
            tmpInventTransWMS.ItemId        = inventTranslocal.ItemId;
            tmpInventTransWMS.InventQty     = _purchParmline.ReceiveNow;
            tmpInventTransWMS.insert();
     
            inventTransWMS_register.writeTmpInventTransWMS(tmpInventTransWMS,
                                                           inventTranslocal,
                                                           inventDimlocal);
            inventTransWMS_register.updateInvent(inventTranslocal);
        }
     
        }
     
  • Martin Dráb Profile Picture
    238,734 Most Valuable Professional on at
    Please tell us more about the behavior. Is there any unhandled exception at all? Maybe there is nothing to catch. Do you see execution of coded interrupted at some place and all the remaining code skipped? If so, where is it thrown? What type it is?
     
    If it's an X++ error, the code for testing it may look like this:
    try
    {
        throw error("Test error");
    }
    catch (Exception::Error)
    {
        info("Caught");
    }
     
     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 577 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 309

#3
Diego Mancassola Profile Picture

Diego Mancassola 259

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans