Tuesday, September 28, 2021

Try and catch in d365FO

 

Code:

        #OCCRetryCount

    System.Exception ex;
try { } catch (Exception::Deadlock) { retry; } catch (Exception::UpdateConflict) { if (appl.ttsLevel() == 0) { if (xSession::currentRetryCount() >= #RetryNum) { throw Exception::UpdateConflictNotRecovered; } else { retry; } } else { throw Exception::UpdateConflict; } }     catch  { ex = CLRInterop::getLastException().GetBaseException(); error(ex.get_Message()); } ===========================OR====================================== try { // your code. }     catch (ex) { System.Exception e = ex; while (e != null) { errorMessage += e.Message; e = e.InnerException; } if (appl.ttsLevel() > 0) { ttsabort; } checkFailed("Process failed"); error(errorMessage); } catch (Exception::Deadlock) { continue; } catch (Exception::Error) { continue; } catch (Exception::Warning) { CLRInterop::getLastException(); continue; } catch (Exception::CLRError) { CLRInterop::getLastException(); continue; } -----------------------OR---------------------------------------------------------------             try { this.createRecord(); } catch (Exception::Deadlock) { retry; } catch (Exception::UpdateConflict) { if (appl.ttsLevel() == 0) { if (xSession::currentRetryCount() >= #RetryNum) { throw Exception::UpdateConflictNotRecovered; } else { retry; } } else { throw Exception::UpdateConflict; } } catch(Exception::DuplicateKeyException) { if (appl.ttsLevel() == 0) { if (xSession::currentRetryCount() >= #RetryNum) { throw Exception::DuplicateKeyExceptionNotRecovered; } else { retry; } } else { throw Exception::DuplicateKeyException; } } catch (Exception::Error) { // infolog.add(Exception::Error, strFmt("", SalesId)); continue; } catch (Exception::CLRError) { ex = CLRInterop::getLastException(); Infolog.add(Exception::CLRError, ex.ToString()); continue; }
---------------------------OR----------------------------------------

With Retry: 

   System.Exception ex; try { } catch (ex) {     if (ex.Message == "Unauthorized401 error" && xSession::currentRetryCount() <= 2)     { // refreshing the token new ServiceClass().processOperation(); retry;     }     if (ex is System.Net.WebException)     { webResponse = (ex as System.Net.WebException).Response;     }     else     {      throw ex;     }     if (webResponse == null)     { throw new System.Exception("UnhandledErrorOccurredDuringTheHttpRequest");     } }

------------------------OR-----------------------------------------------------

With Finallay: 
    while generating reports catch will not trigger. So I have used finally it to fetch the error.

        System.Exception ex; boolean isSuccess; try { //your code isSuccess = true; } catch (ex) { isSuccess = true;     ex = CLRInterop::getLastException().GetBaseException();     throw Global::error(ex.get_Message()); } finally { if (isSuccess == false) { ex = CLRInterop::getLastException().GetBaseException(); Global::error(ex.get_Message()); } }

       Keep daxing!!

No comments:

Post a Comment