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 :
Microsoft Dynamics AX (Archived)

Throw an error to batch but have it handled correctly

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Guillaume Cantin Profile Picture
    689 on at

    Maybe I don't understant the scenario properly, but couldn't you set some kind of flag rather than throwing an exception? This wouldn't break the execution flow and you would be able to react accordingly once you have completed the execution if there is an error. If not, you can put a try/catch statement in the highest level to catch the specific error and treat it, but this will slow your execution in general.

  • Keith Hirst Profile Picture
    455 on at

    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.

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

    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
    455 on at

    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
    237,965 Most Valuable Professional on at

    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
    455 on at

    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
    237,965 Most Valuable Professional on at

    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
    455 on at

    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
    237,965 Most Valuable Professional on at

    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
    455 on at

    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.

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans