Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Exceptions in micro python

    Scheduled Pinned Locked Moved MicroPython
    micropythonexceptions
    12 Posts 4 Posters 1.8k Views 5 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E Offline
      Eric73 @kjm
      last edited by

      @kjm in documentation function prototype is

      sys.print_exception(exc, file=sys.stdout)
      

      Have you try to open a file and use it with sys.print_exception to see what happened inside the file?
      (Sorry i have no module to test)

      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @Eric73
        last edited by

        @eric73 just tried thst on windows MP. If you open a file beforehand, you can use that file handle as second argument to sys.print_exception.

        1 Reply Last reply Reply Quote 0
        • K Offline
          kjm
          last edited by

          not on the gpy you can't!

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @kjm
            last edited by

            @kjm I'll have to check that tonight. I have no device here at the moment.

            1 Reply Last reply Reply Quote 0
            • K Offline
              kjm
              last edited by

              Anybody out there manage to store traceback line numbers to a file on the gpy?

              robert-hhR 1 Reply Last reply Reply Quote 0
              • robert-hhR Offline
                robert-hh @kjm
                last edited by

                @kjm You could do as follows:

                try:
                    1/0
                except Exception as e:
                    import sys
                    with open("error.log", "a") as f:
                        sys.print_exception(e, f)
                

                This works for me,
                Pycom MicroPython 1.20.0.rc6 [8d824a0-dirty] on 2019-01-28; FiPy with ESP32

                1 Reply Last reply Reply Quote 1
                • K Offline
                  kjm
                  last edited by

                  Ah, perfect! Tanks muchly.

                  1 Reply Last reply Reply Quote 0
                  • K Offline
                    kjm
                    last edited by kjm

                    Hey Robert I need to filter out the double inverted commas before I store in the error.log file I tried

                    err=sys.print_exception(e)
                    err=err.replace('"',"")
                    

                    but I got AttributeError: 'NoneType' object has no attribute 'replace'

                    robert-hhR 1 Reply Last reply Reply Quote 0
                    • robert-hhR Offline
                      robert-hh @kjm
                      last edited by

                      @kjm follow these steps. I just executed at the prompt:

                      >>> from uio import StringIO
                      >>> import sys
                      >>> s = StringIO()
                      >>> try:
                      ...     1/0
                      ... except Exception as e:
                      ...     sys.print_exception(e, s)
                      ...
                      >>> x=s.getvalue()
                      >>> x
                      'Traceback (most recent call last):\n  File "<stdin>", line 2, in <module>\nZeroDivisionError: divis
                      ion by zero\n'
                      >>>
                      
                      1 Reply Last reply Reply Quote 0
                      • K Offline
                        kjm
                        last edited by kjm

                        Thnx again Robert, for anyone else with a similar requirement I ended up with....

                        except Exception as e:
                          import sys; from uio import StringIO
                          s=StringIO(); sys.print_exception(e, s)  
                          s=s.getvalue(); s=s.split('\n')                                                                   
                          line=s[1].split(','); line=line[1]; error=s[2]; err=error+line; print(err)
                          with open ('error.log', 'a') as f: f.write(err) 
                        
                        1 Reply Last reply Reply Quote 1
                        • R Offline
                          ronald gevern Banned @kjm
                          last edited by

                          This post is deleted!
                          1 Reply Last reply Reply Quote 0

                          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                          With your input, this post could be even better 💗

                          Register Login
                          • 1 / 1
                          • First post
                            Last post

                          Pycom on Twitter

                          Pycom