With Python try - except is method to catch errors https://docs.python.org/3/tutorial/errors.html. Except should not be too broad, but excepts can be nested to cover most known errors. Programmer shall try to figure out all scenarios where error may raise.
What about random errors, which programmer did not cover in the try - except scenario? Should programmer implement some sort of "de facto catch all exceptions" with main execution? Problem is even more complex to handle with ULP scenarios, where processor sleeps most of the time and then seldomly something goes wrong.
I had in this code https://github.com/divergentti/airquali ... or/main.py error which I was not able to catch via WebREPL. Every now and then the code crashed and processor did not sleep.
Finally, after trying to understand what may fail and tested all kinds of things, I implemented boottimelogger so that I see in which part of the code crashes. From the boottimelog I figured that seldom crahses has to be related to solve_dst_and_set_time() and indeed, after running several times solve_dst_and_set_time() I got a ValueError, which was not covered by the try - except. Something goes wrong with ntptime.settime(), which I have not yet figured. Relates to long integers (error: npttime.settime() error overflow converting long int to machine word)
From bug hunting point of view, try - except is most likely not best practise. Boottime logging seems to work as I implemented it, but best apporach might be logging console to logfile, but how to catch code breaking codes to the logfile? Or is there some better ways to hunt bugs?
Best method to catch random errors?
-
- Posts: 67
- Joined: Fri Sep 04, 2020 9:27 am
- Location: Hanko, Finland
- Contact:
Best method to catch random errors?
Last edited by Divergentti on Sun Feb 28, 2021 8:21 am, edited 1 time in total.
Re: Best method to catch random errors?
Here’s an example of catching an exception and logging to a file: https://github.com/dhylands/upy-example ... int_exc.py
-
- Posts: 67
- Joined: Fri Sep 04, 2020 9:27 am
- Location: Hanko, Finland
- Contact:
Re: Best method to catch random errors?
Thank you. I did not know sys.print_exception. I had:dhylands wrote: ↑Sat Feb 27, 2021 3:08 pmHere’s an example of catching an exception and logging to a file: https://github.com/dhylands/upy-example ... int_exc.py
Code: Select all
if __name__ == "__main__":
f4.write("Execute main\n")
try:
main()
except Exception as e:
f4.write("Exception %s" % e)
if type(e).__name__ == "MQTTException":
if DEBUG_ENABLED == 1:
print("** ERROR: Check MQTT server connection info, username and password! **")
error_reporting("ERROR: Check MQTT server connection info, username and password!")
f4.close()
raise
elif type(e).__name__ == "OSError":
if DEBUG_ENABLED == 1:
print("OSError %s! Booting in 10 seconds." % e)
f4.close()
error_reporting("OSError %s" % e)
sleep(10)
reset()
elif type(e).__name__ == "MemoryError":
if DEBUG_ENABLED == 1:
print("Memory Error %s! Booting in 10 seconds." % e)
f4.close()
sleep(10)
reset()
elif type(e).__name__ == "KeyboardInterrupt":
f4.close()
raise
Re: Best method to catch random errors?
Hi Dave,
I think one of my last problems I need to tackle is catching random errors. I am using the "Simple software WDT implementation" viewtopic.php?t=5517&start=10. I have modified it so that I get the time that the watchdog times-out. This usually helps to find problems when periodically calling another .py file but it doesn't help when my 300-400 line logging program "falls-over".
Any suggestions?
Thanks
I think one of my last problems I need to tackle is catching random errors. I am using the "Simple software WDT implementation" viewtopic.php?t=5517&start=10. I have modified it so that I get the time that the watchdog times-out. This usually helps to find problems when periodically calling another .py file but it doesn't help when my 300-400 line logging program "falls-over".
Any suggestions?
Thanks