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 :
Finance | Project Operations, Human Resources, ...
Unanswered

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

(0) ShareShare
ReportReport
Posted on by 43
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
    236,970 Most Valuable Professional on at
    Try and Catch - Exception handling. Error is not getting caught in Catch part
    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
    43 on at
    Try and Catch - Exception handling. Error is not getting caught in Catch part
    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();
     
    I am curious, The code where the error is being issued should be present IN the try block? Instead of it being called through another class method like here productReceiptImport.run(purchLine, batchId, qtys);  In that case, i can revise the code, so all relevant methods are present in the batch itself. Instead of going through another class.
     
    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.
     
     

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade 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... 703 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 582 Most Valuable Professional

#3
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 542

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans