Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to catch an "object not initialized" exception in Dynamics AX?
    text
    copied!<p><strong>Problem:</strong></p> <p>I have some code that is failing because an object has not been initialized. The solution for this bug is easy to fix, when detected. However, what surprised me is that my elaborate exception handling didn't catch this exception. That meant the exception wasn't logged or handled, and code following the try catch block was never executed. The try...catch block was outside of the transaction, so there was no issue there.</p> <p>In this particular case, the exception was inside a batch (RunBaseBatch) job. The job handled several unrelated processing tasks. Once the exception conditions were met, the job terminated, so the other unrelated processing tasks were never called.</p> <p>Does anyone know if it is possible to catch an "object not initialized" exception in Dynamics AX 2009? I read one post that said it may not be possible to catch certain exceptions in AX, however, I hope that is not the case (reference: <a href="https://community.dynamics.com/product/ax/f/33/p/16352/23700.aspx#23700" rel="nofollow">https://community.dynamics.com/product/ax/f/33/p/16352/23700.aspx#23700</a>).</p> <p><strong>Code example:</strong> Here is some simplistic code that recreates the issue:</p> <pre><code>server static void main(Args args) { Array arr; ; info ("debug: before try...catch"); try { // ttsbegin; // enable/disable to test with transactions // arr = new Array(Types::String); // Enabling this line will prevent the exception arr.exists(3); // ttscommit; // enable/disable to test with transactions } catch (Exception::Internal) // This exception handler was the Magic Sauce!! { info ("debug: catch (Exception::Internal)"); } catch (Exception::Error) { info ("debug: catch (Exception::Error)"); } catch { info ("debug: catch"); } info ("debug: after try...catch"); } </code></pre> <p><strong>UPDATE 2013-01-29</strong> I am waiting to accept an answer until this question has been viewed more. Thank you for the answers so far. </p> <p>I know the example I gave was simplistic. This type of bug is easily fixable when it is known. And defensive programming is always a good idea. </p> <p>However, in the real world, the code where the bug occurred was very complex. The error occurred several levels deep in an overloaded method of a subclass. It occurred in a specific scenario, when an overloaded method corrupted the protected value of a member variable from the super class. That is where the bug occurred in the code, however, it didn't manifest itself until the super class tried to use the member variable again. The bug was summarily fixed when it was detected and tracked down. </p> <p>Defensively, yes you could check every protected member variable, every time you use it, but that does start to impact performance, code readability, practicality, etc., which is why languages offer exception handling.</p> <p>The question here, is <strong>how can these type of bugs be caught to make code more robust and bullet-proof</strong>? In most development environments (C, C++, C#, or Java for example), a try...catch at a top level could be used to catch, log, and clean up <strong>ALL</strong> unexpected exceptions. So the code would be able to continue processing with the other unrelated tasks. AX is continuing at some level, because the whole system doesn't come to a grinding halt when this bug occurs. However, the code after the catch in this job is not executing because of what appears to be a deficiency in AX/X++. </p> <p>I am looking for an innovative solution or work-around, if it exists, to catch the "object not initialized" exception (really ALL exceptions) and to continue processing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload