Writing exception to file

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kbrenner
Posts: 46
Joined: Mon Jan 20, 2020 8:05 pm

Writing exception to file

Post by kbrenner » Tue Jun 30, 2020 5:31 pm

Hi,

I have one big while loop in my script (that calls other modules), and I want to throw one big try/except around that while loop. I'd like to catch the exception and write the error along with the location of the error to file on the Pyboard (probably in some sort of .txt file in flash or on sd). I am running the Pyboard off of external power and will not be able to monitor the REPL prompt, so I need it to write to file so that I can access it later. Are there any suggestions? Right now, I have something like:

try:
#all of the main code
except Exception as e:
writeErrorMessage(e,"Error_Message.txt")

def writeErrorMessage(self,error,fname):
f_handle = open(fname, 'w')
f_handle.write(str(error))
f_handle.close()


kbrenner
Posts: 46
Joined: Mon Jan 20, 2020 8:05 pm

Re: Writing exception to file

Post by kbrenner » Tue Jun 30, 2020 7:15 pm

Thanks dhylands, that worked!

Post Reply