Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

How to get Exception type outside Catch() ?

Posted on by 396
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,204 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,955 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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,275 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans