Python Forum
python exception handling handling .... with traceback
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python exception handling handling .... with traceback
#1
Star 
Hi Team,

I am using below code getting error message.
But I want error properly line by line. I wan to print that error properly line by line.


import traceback
try:
    print(4/0)
except ZeroDivisionError:
   print(traceback.format_exc())
Error:
D:\Python\venv\Scripts\python.exe D:\Python\m12.py Traceback (most recent call last): File "D:\Python\m12.py", line 4, in <module> print(4/0) ZeroDivisionError: division by zero
Expected output:
Output:
File "D:\Python\m12.py" line 4 print(4/0) ZeroDivisionError: division by zero
Reply
#2
Hi Team,

I found little bit of solution , which I was looking for.
I am splitting errors message into list. and printing each value.

is there any alternate easy solution. to get correct info. thanks in advance !
import traceback
try:
    print(4/0)
except ZeroDivisionError:
   error = traceback.format_exc()
   for e in (error.split('\n')):
       if not "Traceback" in e:
           print(e)
Thanks
mg
Reply
#3
These look the same to me.
import traceback

def func(x):
    try:
        return x/0
    except ZeroDivisionError as e:
        print("######################")
        traceback.print_tb(e.__traceback__)
        traceback.print_exception(e)
        print("######################")
        error = traceback.format_exc()
        for e in (error.split('\n')):
            if not "Traceback" in e:
                print(e)
        print("######################")
        return None

def intermediary(x):
    return func(x)

print("func(0) =", intermediary(0))
print("Got here!")
I do not like that the traceback is cuts off at the try. It doesn't show that func() was called by intermediary() which in turn was called from main.
Reply
#4
You could perhaps use specialized modules that people wrote to prettify the Python traceback such as this one (untested)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File Handling not working properly TheLummen 8 752 Feb-17-2024, 07:47 PM
Last Post: TheLummen
Question Why isn't getops handling all parameters? Calab 7 1,100 Feb-03-2023, 07:47 PM
Last Post: deanhystad
  painfully slow runtime when handling data dadazhu 3 975 Jan-20-2023, 07:11 PM
Last Post: snippsat
  file handling Newbee question middlecope 2 784 Jan-18-2023, 03:09 PM
Last Post: middlecope
  Python Traceback Error mg24 2 952 Nov-21-2022, 02:35 PM
Last Post: snippsat
Question log.exception() without arguments in old Python versions? cthart 5 1,176 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
  Help needed with a "for loop" + error handling tamiri 2 2,530 May-27-2022, 12:21 PM
Last Post: tamiri
  File handling issue GiggsB 4 1,448 Mar-31-2022, 09:35 PM
Last Post: GiggsB
  How can I solve this file handling issue? GiggsB 17 3,594 Feb-14-2022, 04:37 AM
Last Post: GiggsB
  Handling pdf files with python. fuzzin 1 1,270 Jan-19-2022, 02:24 PM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

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