Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / How to get Exception t...
Finance forum
Answered

How to get Exception type outside Catch() ?

Posted on by 390
Hi,
 
In my code currently the Try and Catch() does not specifically put the Catch in several Exception type,  If I'm looking at other code or blogs, usually it would be something like this :
 
str error;try    {        // ...    }    catch (Exception::Error)    {        enumerator = SysInfologEnumerator::newData(infolog.cut());        while (enumerator.moveNext())        {            strc = new SysInfologMessageStruct(enumerator.currentMessage());            exception = enumerator.currentException();            error = strfmt('%1 %2', error, strc.message());        }
 
But currently, I only put like this :
 
try
    {
        ......
     }
catch
     {
          this.errorProcedure();
     }
 
 
And in my errorProcedure, I'm grabbing the error in infolog which then insert the error log into my log table, Something like this :
 
public void errorProcedure()
{
         container   infoData = infolog.infologData();
        Str1260     errorTxt;
       
         for (int i = 1; i <= conLen(infoData); i++)
        {
            container internalInfoData = conPeek(infoData, i);
            if (errorTxt == //)
            {
                errorTxt =  conPeek(internalInfoData, 2);
            }
            else
            {
                errorTxt += /;/ + conPeek(internalInfoData, 2);
            }
        }
        // insert into log table
}
         
Issue is, turns out it doesn't catch some error. For example like inside the Try block, there is incident of calculation with divide by zero. This will result the program exit to Catch block and run my errorProcedure(), but there is no infolog and when debug I found that there is an exception ->
The above screenshot is when I debug in the middle of that errorProcedure steps, so I'm thinking, actually the exception still recognize and there is the message which quite clear, only it doesn't /belong/ to the infolog object.
 
How to actually handle this ? Can we still grab the Exception if we put like this ? Can I also grab the Exception and the message /Attempted to divide by zero/ then concat it to my errorTxt string ?
 
Please help.
 
Thanks
 
 
 
  • Verified answer
    Layan Jwei Profile Picture
    Layan Jwei 7,071 Super User 2024 Season 2 on at
    How to get Exception type outside Catch() ?
    Hi Ken,

    You need to catch the error for it to appear. This is how i prefer to catch exceptions
     
    System.Exception  ex;
    
    try
    {   
    }
    catch(ex)
    {
       error(ex.message);
      this.errorProcedure(); }

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • Suggested answer
    Waed Ayyad Profile Picture
    Waed Ayyad 5,456 Super User 2024 Season 2 on at
    How to get Exception type outside Catch() ?
     
    You can try this code  :
     
    try
    {
    // Your logic
    }
    catch (Exception::CLRError)
    {
       ex = ClrInterop::getLastException();
      if (ex != null)
      {
        ex = ex.get_InnerException();
       if (ex != null)
      {
        error(ex.ToString());
       }
      }
    }
     
     
    Regards,
    Waed Ayyad
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
     

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,532 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,501 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans