Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

catch block not throwing an error

(0) ShareShare
ReportReport
Posted on by 1,550

Hi,

the code reaches catch(ex) but it doesn't get inside and It doesn't print the error inside

catch (ex)
{
    ClassName className = classId2Name(classIdGet(this));
    throw error(strFmt("error in class: %1 ", className));
}

  • Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    RE: catch block not throwing an error

    Please don't forget using Insert > Code to paste source code.

    You don't have 'catch; between ttsbegin and ttsbegin, therefore it'll work, unless the whole code is called from another transaction. Context is important.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: catch block not throwing an error

    Hi Martin,

    When we create a new country..it happens in logisticsAddressSetup form

    There is a "new command button", on clicked method it calls logisticsAddressCountryRegion_ds.create() (tts level still 0) then standard code from formUtil..when it goes to insert I can see level became 1..so where should I catch it?

    When you say after transaction...you mean that try and catch should be after ttscommit?

    So in general..the below won't catch exceptions? And it won't print customized? If yes how to change it to make it print customized?

    Try

    {

    Ttsbegin;

    Throw error("error");

    Ttscommit;

    }

    Catch(ex)

    {

    Throw error(" customized error");

    }

  • Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    RE: catch block not throwing an error

    As I thought.

    Why exceptions can't be caught in transactions? Because the system is implemented in such a way.

    What you can do about it? Nothing. You just need to accept the reality.

    The place where you can catch exceptions is after the transaction. You clearly have no idea where it is, but if you want to do such a thing, you'll need to find it. Note that you have the sequence of calls in debugger, so you can look at them and find which one added the transaction.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: catch block not throwing an error

    Hi Martin,

    Looking at auto variables after next insert() for country table and before calling classTest

    It says Ttslevel = 1

    I'm still not sure what I should do now and why the Ttslevel causes this

  • Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    RE: catch block not throwing an error

    I didn't say that you added a transaction in insert() - I said that insert() is likely called from inside a transaction.

    Look at Auto variables in the debugger to see the transaction level. Or call appl.ttsLevel() in your code. Just trying to guess what's going isn't usually very successful, as you see, therefore it's important to collect facts.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: catch block not throwing an error

    Hi Martin,

    There is no insert method in the standard logisticsAddressCountryRegion table to look if there is transaction

    And in my extension for the insert method, I only added code after insert  to call classTest

    So I'm not sure what is wrong..how can I fix the issue and make customized error appear?

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    RE: catch block not throwing an error

    Let me simplify your code used for demonstration:

    internal final class CatchDemo
    {
        public static void main(Args _args)
        {
            System.Exception ex;
    
            try
            {
                throw error("error");
            }
            catch (ex)
            {
                error("customized error");
            }
        }
    }

    When I run it, the exception gets caught as expected and "customized error" text is added to infolog.

    pastedimage1674547852368v1.png

    Try it and see it for yourself.

    Then look again at the transaction level when calling your code from insert(). I'm pretty sure that you'll find that it is in transaction, which explains the behaviour.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: catch block not throwing an error

    Hi Martin,

    What do you mean type of ex variable...as the code shows in my previous reply to Andre.. it's of type system.exception

    As for the transaction there is no transaction...but the method that calls classTest is the insert for CountryRegion table....where I made an extension of the insert method to call classTest

    What should I do?

  • Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    RE: catch block not throwing an error

    1) It's how the debugger works.

    2) As I already mentioned, it depends on the type of ex variable. It's also possible that your code is in a transaction, where you can't catch (most types of) exceptions.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: catch block not throwing an error

    Hi Martin,

    Why the break point jumped to catch(ex)?

    And isn't throw error("error") captured by catch(ex)?

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,836 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans