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)

Problem with Exception::CLRError using System.IO.StreamWriter

(0) ShareShare
ReportReport
Posted on by 1,080

Hi,

I'm having a weird problem when trying to catch a CRL Exception in AX 2009. I haven't had this problem before with other CLR operations, but when using the System.IO.StreamWriter .NET class and an exception is thrown I'm not able to handle it in a catch statement.

It would be great to have some insight about this.

The following is my code:

try

    {

        new InteropPermission(InteropKind::ClrInterop).assert();

        if(WinApi::fileExists(invoiceFilePath))

            WinApi::deleteFile(invoiceFilePath);

 

sw = new System.IO.StreamWriter(invoiceFilePath); //This is where it crashes and does not fall into the CLRException

 

    //Write in file

 

   ….LOGIC HERE (Too long to post it) ….

 

                sw.Flush();

                sw.Close();

                sw.Dispose();

                CodeAccessPermission::revertAssert();

 

                break;

        }

    }

    catch

    {

        this.eInvoiceProcessTextWriteError();

 

    }

    catch(Exception::Internal)

    {

        this.eInvoiceProcessTextWriteError();

        exc = CLRInterop::getLastException();

        if( exc )

        {

            clrExcMessage = exc.get_Message();

            // BP Deviation Documented

            strError = CLRInterop::getAnyTypeForObject( clrExcMessage );

 

            throw error(strError);

        }

    }

    catch(Exception::CLRError)

    {

        this.eInvoiceProcessTextWriteError();

        exc = CLRInterop::getLastException();

        if( exc )

        {

            clrExcMessage = exc.get_Message();

            // BP Deviation Documented

            strError = CLRInterop::getAnyTypeForObject( clrExcMessage );

 

            innerExc = exc.get_InnerException();

            while(innerExc != null)

            {

                clrExcMessage = innerExc.get_Message();

                // BP Deviation Documented

                strError = strError + '\n' + CLRInterop::getAnyTypeForObject( clrExcMessage );

                innerExc = innerExc.get_InnerException();

            }

 

            throw error(strError);

        }

 

    }

    catch(Exception::Error)

    {

    this.eInvoiceProcessTextWriteError();

    }

 

 

Any help will be appreciated!


*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hi

    First of all, I don´t know if I am correct, but I think that you should sort your "catches" the other way around. The first catch probably catches everything (but I might be wrong...). I would place the specialized catches first.

    I tried you code and the only error I could get was when I entered a path that does not exist. That did not cause a CLR error, since the very initialization of the object failed. The error was logged in the infolog, though.

    I do however remember that I have had the same problem as you have. The CLR call crashes so hard that the exception don´t even get caught. I just can´t remember why and what my solution was.

  • Verified answer
    EduardoAndres Profile Picture
    1,080 on at

    Hi,

    No that wasn't the problem. Anyway, I fugured this out and a workaround can be found here:

    axwonders.blogspot.com/.../workaround-to-exceptionclrerror-using.html

    Thank though!

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