My EAs log diagnostic data in a text file. This helps me troubleshoot users problems, monitor the trade execution (slippage, server roundtrip delay), etc. Unfortunately, my EAs are not perfect and might cause a critical error (e.g. "array out of range"). It would be great if the EA would allow me to also log this before terminating. For less technical users, or users running several EAs in parallel, it is hard to find the error message in the Terminal Journal so this requires a lot of support from my end to guide them.
Can I somehow intercept a critical error before the EA terminates? I noticed the OnDeinit() function is not called, neither are destructors. The example code below does not output anything:
Is there a callback function, or hook I can use to intercept the fatal error?
Interesting idea but the newbie mistakes that throw out of range are easy to find with debugger. The more tricky ones happen at the intersection of two different blocks of logic (only while running).
Can you show us a code that gives you an array out of range error ?
I would suggest to just make sure not to have that error in the first place. You can create for example class functions that handle array insertion and resizing like in the CArray... class. It has been a long time since I got that annoying error.
Thank you for your feedback, but I feel you misunderstood my question. Of course I must properly debug my program before releasing it, or can write to Comment(), make a CArray wrapper class, etc.
My point was, that critical errors (that is, error that halt my EA) result in a message in the Journal tab, but do not allow my EA do something before crashing. The destructors are not called, the OnDeinit() is not called, etc.
The only thing I can do is guide my user through thousands of lines in the Journal log to help him find the diagnostic message I need.
I was hoping for something like @pennyhunter suggested: a OnDebuggerStop() callback, or a POSIX signal handler, etc. Or a means to read back the Journal when the EA is restarted (without enabling .dlls to use Windows File I/O, because my users won't do that).
Thank you for your feedback, but I feel you misunderstood my question. Of course I must properly debug my program before releasing it, or can write to Comment(), make a CArray wrapper class, etc.
My point was, that critical errors (that is, error that halt my EA) result in a message in the Journal tab, but do not allow my EA do something before crashing. The destructors are not called, the OnDeinit() is not called, etc.
The only thing I can do is guide my user through thousands of lines in the Journal log to help him find the diagnostic message I need.
I was hoping for something like @pennyhunter suggested: a OnDebuggerStop() callback, or a POSIX signal handler, etc. Or a means to read back the Journal when the EA is restarted (without enabling .dlls to use Windows File I/O, because my users won't do that).

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
My EAs log diagnostic data in a text file. This helps me troubleshoot users problems, monitor the trade execution (slippage, server roundtrip delay), etc. Unfortunately, my EAs are not perfect and might cause a critical error (e.g. "array out of range"). It would be great if the EA would allow me to also log this before terminating. For less technical users, or users running several EAs in parallel, it is hard to find the error message in the Terminal Journal so this requires a lot of support from my end to guide them.
Can I somehow intercept a critical error before the EA terminates? I noticed the OnDeinit() function is not called, neither are destructors. The example code below does not output anything:
Is there a callback function, or hook I can use to intercept the fatal error?