Announcements
CLRError, could be web Exception or Exception (maybe there might be sth else)
catch(Exception::CLRError) { System.Exception ex = CLRInterop::getLastException().GetBaseException(); error( ex.get_Message());
}
so here if i use System.Exception and it was web exception i will get a null reference...so how do i handle this?
Try to check that it is not null first, like shown here https://docs.microsoft.com/en-us/dynamicsax-2012/developer/how-to-catch-exceptions-thrown-from-clr-objects#ee677495collapse_allen-usax60gifx-code-sample and get details in the same way.
If i remove .getBaseException() as the link says, it now checks if it's null or not instead of getting an error directly
System.Exception ex = CLRInterop::getLastException(); if (ex != null) { }
But if i do the opposite use web exception first without getBaseException(), i get an error before checking if it's null
System.Net.WebException webEx = CLRInterop::getLastException(); if (webEx!=null) { }
So what to do in this case?and what other types of error does CLR catch?
Don't use Exception::CLRError and CLRInterop::getLastException() anymore; it can be done in an easier way (in D365FO, not in older versions):
System.Net.WebException webEx; try { ... } catch (webEx) { error(webEx.Message); }
so i should define all possible scenarios
System.Net.WebException webEx; System.Exception Ex; try { } catch(webEx) { } catch(Ex) { }
What about (Exception::Error)? i used to put catch for clr,and one for error and a generic one
catch(Exception::CLRError) { } catch (Exception::Error) { } catch { }
You should use those you need - no more, no less. I can't tell you whether you need to handle X++ errors in a special way and therefore you need "catch (Exception::Error)" block. It clearly depends on what you're doing. If you don't need it, remove the block. And definitely don't use empty blocks for exceptions that you don't want to handle, because the result would be the opposite (the exception would get handled, there just wouldn't be any extra logic at the time of handling).
I do have logic inside catch(webEx) and catch(Ex) and the generic catch
My question is since u said there is no need to use Exception::clr and use catch (Ex) and catch(WebEx) instead then is there also a way to not use Exception::error? Is there sth else?
You asked about CLR exceptions, so that's what we discussed. But normal, non-CLR exceptions still exist and they can be handled in the usual way. Therefore you can use Exception::Error if you want to catch native X++ exceptions.
Ok one last thing
Since Catch(Ex) can catch both Ex and webEx so instead of doing two catches (where the first catch should be webEx then Ex) can't i from one catch which is Ex inside find they type if it is web or normal exception and do logic accordingly?
Yes, you cam do that, but normally it's easier to simply catch the right type straight away.
If my code in they try is related to web response then is there a need to catch(Ex). I mean if u know the code in the try would Ex occur out of no where? Or a case that i don't know about.. Or in such cases i should put the Exceptions i'm sure of and anything else the general catch should catch
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.
Congratulations to our community stars!
Expanding mentorship, skilling, and AI innovation
These are the community rock stars!
Stay up to date on forum activity by subscribing.
SajeedMullaji 312
André Arnaud de Cal... 284 Super User 2026 Season 2
Martin Dráb 258 Most Valuable Professional