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

Announcements

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

(3) ShareShare
ReportReport
Posted on by 337
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
Email error - Copy.PNG
I have the same question (0)
  • Verified answer
    Jonas "Jones" Melgaard Profile Picture
    5,006 Most Valuable Professional on at
     
    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/
  • MS-29011540-0 Profile Picture
    337 on at
    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

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... 692 Super User 2026 Season 1

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 280 Super User 2026 Season 1

#3
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 244

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans