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, ...
Suggested Answer

catch block not throwing an error

(0) ShareShare
ReportReport
Posted on by 1,552

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));
}

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    300,731 Super User 2025 Season 2 on at
    RE: catch block not throwing an error

    Hi Junior AX,

    The purpose of the catch statement is to capture any error and continue without throwing a warning. If you need to raise an error, then don't use the catch method.

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

    Hi Andre,

    but in case of any error, I want to return a customized error message, I don't want to return ex.Message ... how can I do that without the catch?

    For example, here I want the "customized error" to appear on the UI, instead of the "error" in init Method in class1

    public classTest
    {
        public static void reset(A _a)
        {
            Class1 class1 = Class1::newFromParameters('x', _a);
            class1.send();
        }
    }
    
    public Class1 extends classBase
    {
        public static Class1 newFromParameters(str _request, A _A = null)
        {
            Class1 class1 = Class1::construct();
            Class1.parmRequest(_request);
            Class1.parmA(_a);
    
            return class1;
        }
        
        public void init()
        {
            throw error("error");
        }
    }
    
    public classBase
    {
        public void init()
        {
        }
        public void send()
        {
            System.Exception            ex; 
    
            try
            {
                this.init();
                //logic
            }
            catch(ex)
            {
               error("customized error"); 
            }
        }
    }

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

    If you don't get into the catch block, it means that either no exception was thrown or the type is different than the type of ex variable.

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

    Hi Martin,

    It reaches the catch(ex) as the break point is captured and I can see the error in ex but it doesn't get inside so it doesn't reach the error("customized error") line.

    As you can see in class1 (init method) I throw an error by doing this

    Throw error("error")

    So the type should be captured by catch(ex) in classBase but why it didn't reach the customized error line?

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

    Your assumption that if you see the value in ex variable means that the exception must have been caught makes sense, but it's not correct.

  • junior AX Profile Picture
    1,552 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)?

  • Martin Dráb Profile Picture
    237,697 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
    1,552 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?

  • Suggested answer
    Martin Dráb Profile Picture
    237,697 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
    1,552 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?

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 683 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 563 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 398 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans