Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

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

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

  • Verified answer
    EduardoAndres Profile Picture
    EduardoAndres 1,080 on at
    Re: Problem with Exception::CLRError using System.IO.StreamWriter

    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!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    Re: Problem with Exception::CLRError using System.IO.StreamWriter

    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.

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans