Question regarding getLastError()

 
Hello, quick one: If getLastError() for instance returns 4204 and I don't use ResetLastError() to set it to 0 again. When a new error is produced, for instance, 4020, getLastError() will then return 4020, right? Meaning that I do not have to rest the lastError to receive a new one. It is updated automatically, isn't? Thanks!
 
That is correct.
But if you do not get a new error, the old code will still be present.

How would you distinguish the source?


 
Dominik Egert:
That is correct.
But if you do not get a new error, the old code will still be present.

How would you distinguish the source?


Yes, that was my thought that the old code will be present until a new error occurres.

I do not understand what you mean with "How would you distinguish the source?"

 
Don't look at the last error unless your function call returns an error; you always know the source.
 
William Roeder:
Don't look at the last error unless your function call returns an error; you always know the source.
I am wondering why you would need resetLastError() anyhow. If a native mql5 function throws an error and you always print the error why would you need to reset it?
 
algotrader01 If a native mql5 function throws an error and you always print the error why would you need to reset it?

Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

 
William Roeder:

Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

Hi William! Thanks for your reply again. The last bit ... "Must..." I do not understand sorry. That does not make any sense to me. Would you be kind enough to explain that in other words/more detail as would be of course interested to understand. Thank you!
 
Let's say you call printf("something") and you call another function which returns an error code, but runs successfully and then you check the error code.

You will get an error sourced by printf, but you will relate the error with the second function call.

Check and see.

Then you will understand.
 
There's also a subtle difference between GetLastError() in MQL4 and MQL5. In MQL4 it will automatically reset the _LastError variable. Just in case you're not already confused yet.
 
algotrader01: The last bit ... "Must..." I do not understand sorry. That does not make any sense to me.
  1. You call a function that sets Last Error.
  2. You then call iClose, MarketInfo, etc.
  3. If you now attempt to see if that call failed, you will read the № 1 error code when it succeeds.

You must you call ResetLastError before in order to check after. For all the other calls, they return a status value. You only need to check Last Error if you actually have an error.
          What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
          Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

Reason: