Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Throw an error to batch but have it handled correctly

Posted on by 455

Hi All,

We have a strange problem which we am unable to solve.

We have some code which is running in batch using SysOperationFramework. This piece of code is called from StartOperation which calls the service menu item (textbook implementation).

When it is in batch we are getting throwing an error which we want to handle. If we handle the error by displaying a message then it will not error the batch job (which is something we need to do) To cause the batch to error we need to throw an error at the highest point and this will inform the batch and it will set its status to error, we have added additional code which will then go back to our transaction and update the information when there is an error in batch.

The problem we get here is that by throwing the error at the highest level, it has nowhere to go so throws a stack trace error to the infolog. Displaying stack trace errors to an info log will just cause users to panic so we don't want to do this, but there is no way to get the batch to error unless we throw an error.

Can anyone help? I know it is a confusing scenario but there has to be some way.

Thanks for any help

*This post is locked for comments

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Throw an error to batch but have it handled correctly

    I don't know about any way how to throw an unhandled exception and bypass the default exception handler at the same time. Furthermore, it seems to be normal behavior. If I run LedgerExchAdj batch (for example) without a required number sequence and it's executed in CIL, I get the following error:

    A number sequence has not been set up for the reference Foreign currency revaluation in the area General ledger.
    Microsoft.Dynamics.Ax.Xpp.ErrorException: Exception of type 'Microsoft.Dynamics.Ax.Xpp.ErrorException' was thrown. (...)

    FormletterService, on the other hand, goes in the way of not throwing unhandled exceptions. It tries to handle all exceptions in run() method.

  • Keith Hirst Profile Picture
    Keith Hirst 455 on at
    RE: Throw an error to batch but have it handled correctly

    Yeah that is exactly my error. The error is from some validation you see so it fails and we want it to fail the batch as well. If the user sees a stack trace then they will panic so we don't want it to display.

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Throw an error to batch but have it handled correctly

    Thanks. You can see it's an exception thrown by CIL. You're wrong in assuming that the difference is whether you run your code in batch or not, the difference it whether you run X++ or CIL generated from X++.

    If I have code like this:

    try
    {
        throw error("Error");
    }
    catch
    {
        throw error("Caught");
    }

    and run it in X++, infolog contains the following:

    Error
    Caught

    If I run it in CIL, it contains this:

    Error
    Caught
    Microsoft.Dynamics.Ax.Xpp.ErrorException: Exception of type 'Microsoft.Dynamics.Ax.Xpp.ErrorException' was thrown. (...)

    Do I understand correctly that your problem is that you don't like the last line in infolog?

    To the off-topic discussion about Info class - the class is about AX client, not merely about infolog. It has methods such as startup(), shutDown(), workspaceWindowCreated(), processId() and so on. Look yourself.

  • Keith Hirst Profile Picture
    Keith Hirst 455 on at
    RE: Throw an error to batch but have it handled correctly

    Sorry I didn't know what a stack trace would be able to tell you so thought you meant the exception.

    Microsoft.Dynamics.Ax.Xpp.ErrorException: Exception of type 'Microsoft.Dynamics.Ax.Xpp.ErrorException' was thrown.

    ...

      at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeInstanceCall(Object instance, String MethodName, Object[] parameters)

      at Dynamics.Ax.Application.SysOperationServiceController.Runoperation(Boolean _async) in SysOperationServiceController.runOperation.xpp:line 88

      at Dynamics.Ax.Application.SysOperationServiceController.Run() in SysOperationServiceController.run.xpp:line 27

    ...

      at Dynamics.Ax.Application.BatchRun.runJobStaticCode(Int64 batchId) in BatchRun.runJobStaticCode.xpp:line 54

      at Dynamics.Ax.Application.BatchRun.runJobStatic(Int64 batchId) in BatchRun.runJobStatic.xpp:line 13

      at BatchRun::runJobStatic(Object[] )

      at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type type, String MethodName, Object[] parameters)

      at BatchIL.taskThreadEntry(Object threadArg)

    I don't understand how that method would be part of the infolog class if it closes the client session. Or is it extended from object?

    Thanks

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Throw an error to batch but have it handled correctly

    Yes, I know it's an error, but I asked about which error it is. Please post here the whole content of infolog so I can see whether it's the CLR exception or something else.

    autologOff() is related to automatic closing of a client session, it has nothing to do with displaying messages in infolog.

  • Keith Hirst Profile Picture
    Keith Hirst 455 on at
    RE: Throw an error to batch but have it handled correctly

    It is an exception of type error. Basically i have a try catch around some code which does validation and when that validation fails it displays an error info log message which triggers the catch. This then throws another error so it is caught in main which again throws so that the batch can be aware.

    I read the that the CLR errors don't get displayed to the infolog so as an experiment I tried to throw one in main and when I checked the batch log it still displayed the stack trace.

    When i was messing around I saw there is a method 'infolog.autologOff();' which I would imagine would suppress any more infologs from displaying but there doesnt seem to be a way to turn it on after so I didn't want to use it. As usual theres no information about this method on the msdn.

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Throw an error to batch but have it handled correctly

    What's the error you get? I guess it's the CLR infolog exception (I don't remember its exact name), but I would like to hear from you what exception are you talking about.

  • Keith Hirst Profile Picture
    Keith Hirst 455 on at
    RE: Throw an error to batch but have it handled correctly

    Hi Martin,

    I am throwing a new exception from my catch which does cause the batch to fail but that is also causing a stack trace error to be shown in the log.

    Something interesting to note is that the stack trace only gets called when you are running the code in batch, if you don't choose batch processing then it will just throw the error message we are expecting.

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Throw an error to batch but have it handled correctly

    I didn't get what you're saying. If you handle errors in main(), it won't happen in batch, because batch doesn't call main of the batch class - it calls the operation directly. Main is there just to initialize the class and show a dialog to users, so they can choose what will run in batch.

    Also, if you handle an exception but your problem is that it prevents batches from failing, simply throw a new exception from your "catch" clause.

  • Keith Hirst Profile Picture
    Keith Hirst 455 on at
    RE: Throw an error to batch but have it handled correctly

    Thanks for your reply.

    It is really strange how the system is handling this but I will try to explain better.

    You have your startOperation command (client side) which sends a particular piece of code into batch (server side). This piece of code is recieving an error which we are handling correctly but because it is on the server the highest level is the main method and the error is being handled there.

    What appears to happen next is that the batch process picks an error up if it is thrown so that it can set its own state to error. It doesnt seem to handle anything and when debugging through visual studio, the error does not hit the batch processing so this must be run as part of a seperate instance.

    So therfore we are having to throw the error in the main so the batch updates but by doing that the system will take this as an error and will automatically display a stack trace error message which we can't have the user seeing.

    Ideally there would be a way in the system to supress the stack trace error.

    Does this make it clearer? Sorry if not it is a very confusing thing to explain.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans