Python Forum
an exception traceback and continue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
an exception traceback and continue
#1
i would like to try a given block of statements, and if there is any exception, just keep on running like:
    try:
        whatever goes here
        ...
    except:
        pass
but ... i'd also like to get the same full traceback and exception message output like i would get if i did not use the try/except and continue running. in another case i'd like to have only the exception message output without the traceback. in a 3rd case i'd like to capture the traceback and exception message into some data object. in a 4th case same as the 3rd but only capture the message. the 4th case will not be needed if the is some way to extract the message from the whole capture.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I posted a snippet on manipulating the call stack, which is what a traceback does,
here: https://python-forum.io/Thread-Walking-t...hout-leaks
Reply
#3
You can invoke the functions from the traceback module to obtain this, for example
import traceback as tb

while True:
    n = input('Give me an integer: ')
    try:
        n = int(n)
    except ValueError:
        tb.print_exc()
    else:
        print('Got:', n)
Reply
#4
Sound like a logging setup.
Example:
import my_log
 
def add(x, y):
   try:
       return(x + y)
   except Exception as error:
       my_log.logger.exception('msg')
 
if __name__ == '__main__':
   my_log.logger.info('Start')
   value = add(33, '50')
   my_log.logger.debug(value)
   my_log.logger.info('Finish')
   print(value)
One run without error and one with '50'
There are many cases where not give all error message to users make sense,
server,GUI...ect.
logg.log:
Output:
2018-01-17 08:35:50,623 - my_log - INFO - Start 2018-01-17 08:35:50,623 - my_log - DEBUG - 83 2018-01-17 08:35:50,623 - my_log - INFO - Finish 2018-01-17 08:36:18,791 - my_log - INFO - Start 2018-01-17 08:36:18,791 - my_log - ERROR - msg Traceback (most recent call last): File "_logging.py", line 5, in add return(x + y) TypeError: unsupported operand type(s) for +: 'int' and 'str' 2018-01-17 08:36:18,791 - my_log - DEBUG - None 2018-01-17 08:36:18,795 - my_log - INFO - Finish
setup my_log.py:
Reply
#5
i'm wanting to do some things with this for debugging tools.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,216 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  During handling of the above exception, another exception occurred Skaperen 7 26,727 Dec-21-2018, 10:58 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020