Skip to main content

Notifications

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

  • Verified answer
    EduardoAndres Profile Picture
    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
    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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics AX (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 100 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 48

#3
Zain Mehmood Profile Picture

Zain Mehmood 6 Moderator

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans