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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Is it correct to have multi-level Try..Catch ?

(0) ShareShare
ReportReport
Posted on by 596

Hi guys,

Recently I've been doing some modification on someone else's class, which I'm adding further methods. The point is, in my new class, I put Try..Catch() while inside is calling the "old" class method which inside has it's own Try..Catch(). I found out if there is error on the "inner" class, the error "catch" over there, and in upper class will not get error catch again. Is that true ? 

How is the better approach of this ? I mean in that upper class, I may need to have error procedure also, if the inner class has error, for example I have a Log table that I need to insert/update inform the process failed, it feels odd to have my Log table in Catch procedure of the inner class instead.

Thanks,

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

    Just saying that there is a catch clause isn't enough. There may be a catch clause, but catching a different type of exceptions, therefore irrelevant to your scenario. Or there it might catch an exception, but throw it again - that there is a catch clause doesn't mean that it'll actually handle the exception. Also, that there is a catch clause doesn't mean that it'll be ever used - it'll be ignored in database transactions (for more exception types), which may be a problem, but you can also benefit from it.

    If the inner class handles the exception you're interested (and continues the execution), there is nothing to catch in your upper class. The upper class called the inner class, which completed without throwing an exception. There is no failure to log. If you think it's a wrong design, the best approach likely is changing the code. You could also call the inner class in a transaction, therefore the catch clause there would be ignored, but such a solution may be hard to understand and maintain.

  • VoltesDev Profile Picture
    on at

    Yes,

    I think you are right. If I noticed it correctly, the inner class has an error that actually no appropriate Catch available. But how to know which Catch should we put ? 

    The inner class currently has these catch() : 

    • catch(Exception::Error)
    • catch (Exception::UpdateConflict)
    • catch (Exception::DuplicateKeyException)

    The function causing the error is because in that inner class trying to posting invoice a sales order by calling the standard SalesFormLetter. And actually the error message is captured "Posting Sales order: SO-DT-000000574 Voucher INV-S-DT-000000367 You must select a value in the GROUP field in combination with the following dimensions values that are valid:"

    I understand this error because the Sales Order about to post invoice has no Financial Dimension -> GROUP specified, but I guess it is not as an Expection::Error. So what type of error catch should we put ?

    Thanks,

  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

    Don't speculate - test it. You can see whether an exception gets raised (I think it was, although it might have been handled), whether it's captured by the catch(Exception::Error) block (I would expect so) and whether the exception is handled there (it depends on code inside the clause, which you didn't show). The debugger can also show you what kind of exception was thrown and where and whether you're in a transaction.

    You seem to be suggesting that an exception gets thrown and it's not handled by the inner class, but you said before that it is.

  • Voltes Profile Picture
    596 on at

    Hi Martin,

    Ye, after several test, it is true the catch(Exception::Error) in the inner class is raised, The "undhandled error" though, now I realized is coming from the standard AX class. 

    pastedimage1673428636655v1.png

    This is actually from FormLetterService Class. May I know if we can see what kind of exception from that image, because it only says ErrorException ?

    However, actually it is fine, since now I know the catch(Exception:Error) in inner class is working, but to my earlier question is, since this is handled, why after it is move back to my class (the outer class), it is continuing to my next statement of the code ?

    Inside my class (outer class), I'm calling the inner class with something like this : 

    try
    {
       My_SalesPosting = new My_SalesPosting();
       My_SalesPosting().postSalesInvoice();
    
        .
        .
        
    }
    catch
    {
    }
    

    So inside that My_SalesPosting(), now it is cleared that it was hit error and handled by its own catch(Exception:Error), so when the process return back to my class (because of an error), it is consider the inner class completed ? so it is continuing to my next code ?

  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

    The type is ErrorException. If you want to see details, check out properties of the object, such as Message and StackTrace.

    I'm not sure what you mean by your question "why it moves to my statement of code if it's been handled". It's the expected thing, isn't it? Execution would be interruputed if the exception wasn't handled, but handling an exception means that execution should continue.

  • Voltes Profile Picture
    596 on at

    Oh I guess, I misunderstood then. I thought my class (outer class) will be "jump" to error as well (which actually what I'm expecting). 

    Noted on this.

    So I guess to have Try..Catch() also in my class does not help, is it ? how to have the inner class "inform" my class that is has error, without I need to check the data whether it is updated or not ? I mean can it handled by something ?

    Thanks.

  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

    No, it won't. If the inner class handles the exception (by catching and not rethrowing it), there is no exception thrown by the inner class and therefore no exception to catch by the upper class.

  • Voltes Profile Picture
    596 on at

    Rethrow means in the inner class, while catch(Exception::Error), in some part of the block, I should add throw error("") ? so then in the outer class will catch the same Exception ?

  • Verified answer
    Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

    Yes, a throw statement inside a catch block. Typically throw error(...), but there are other options too (such as throwing an exception object or Exception::Error).

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
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 241 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans