Skip to main content

Notifications

Community site session details

Community site session details

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

Why are my x++ exceptions not being caught d365fo

(2) ShareShare
ReportReport
Posted on by 83
I created the following method for sending emails:
 
    public static void sendEmail(str templateId, str emailTo, Map templateToken)
    {
        Email   emailFrom, subjectTxt;
        str     emailFromName, emailBody;
        var mailer          = SysMailerFactory::getNonInteractiveMailer();
        var messageBuilder  = new SysMailerMessageBuilder();
        [subjectTxt, emailBody, emailFrom, emailFromName] =
                SendEmailHelper::getEmailTemplate(templateId);

        if (emailTo != '')
        {
            System.Exception e;
            try
            {
                messageBuilder.reset()
                 .addTo(emailTo)
                 .setSubject(subjectTxt)
                 .setBody(SysEmailMessage::stringExpand(
                    emailBody, SysEmailTable::htmlEncodeParameters(templateToken)))
                    .setFrom(emailFrom);
                mailer.sendNonInteractive(messageBuilder.getMessage());
            }
            catch(e)
            {
                Error(strFmt("Error: %1", e.ToString()));
            }
        }
    }
But the problem when the sender user doesn't have the permission for sending the email sendNonInteractive method throw error, but it isn't caught in the catch clause despite using the parent class of all exceptions, can you please tell me what may be the reason?
It seems as if the exception is swallowed or rethrown in the catch clause but basically it doesn't enter the catch clause
  • MS-29011540-0 Profile Picture
    83 on at
    Why are my x++ exceptions not being caught d365fo
    Hello @ ,
    Thanks for your reply
    I think your assumption of that the code may be used in TTS is correct because i use it in OnUpdating eventhandler for ledgerJournalTable,
    I use the code on OnUpdating methpd to be able to check the orig buffer with the current buffer to check if the journal is posted or not
  • Verified answer
    Jonas "Jones" Melgaard Profile Picture
    4,257 Super User 2025 Season 1 on at
    Why are my x++ exceptions not being caught d365fo
     
    Have you checked if this code is being called inside of a transaction (TTS) scope?
    If so, only a few handful exceptions related to transaction integrity can be caught.
     
     
    Edit: I see you have added the exact exception, and now see it's related to .net. My assumption about TTS might be incorrect, if so Martin wrote a really great article about handling different kinds of exceptions thrown by .net: https://dev.goshoom.net/2017/06/catching-exceptions-in-ax-7/

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Martin Dráb Profile Picture

Martin Dráb 601 Most Valuable Professional

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 416

#3
Adis Profile Picture

Adis 384 Super User 2025 Season 1

Product updates

Dynamics 365 release plans