call stack on application exception not returning full stack
Hi all,
I'm trying to improve the error handling of my scripting. Sometimes I have a try/catch in my code to handle some application exceptions e.g. trying to edit a readonly file
Current example:
function LogError() { try { LogErrorStack1(); } catch(e) { LogException(e,"Perhaps you should try that again"); } } function LogErrorStack1() { try { LogErrorStack2(); } catch(e) { throw e; } } function LogErrorStack2() { try { throw("test exception"); } catch(e) { throw e; } } function LogException(exception, additionalInformation) { Log.Error("An application exception was encountered: \n" + exception,additionalInformation); //More work for different exceptions }
I would expect the call stack to be LogException -> LogError -> LogErrorStack1 -> LogErrorStack2
Instead the stack ends at LogError:
It has missed out two of the functions in the call stack. Thoughts on how I can them displayed as well without logging on every tier of the call stack (LogErrorStack1 and LogErrorStack2 catches)?
That's because the Call Stack is recorded at hte moment that "Log.Error" is called.... and at that point, the only "stack" that is there is the initial LogError call and LogException call. If you want to see the stack that you are requesting, you should change your code to this so that the Log.Error is at the very bottom of your stack.
function LogError() { try { LogErrorStack1(); } catch(e) { //Handle Exception } } function LogErrorStack1() { try { LogErrorStack2(); } catch(e) { throw e; } } function LogErrorStack2() { try { throw("test exception"); } catch(e) { LogException(e,"Perhaps you should try that again"); } } function LogException(exception, additionalInformation) { Log.Error("An application exception was encountered: \n" + exception,additionalInformation); //More work for different exceptions }