Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:47:31+00:00 2026-06-15T05:47:31+00:00

I have some code where I parse a file. It’s simple, like so: for

  • 0

I have some code where I parse a file. It’s simple, like so:

for line in config_file:
    line_split=line.split("|")
    pid_index = int(line_split[3])
    date_locations = [int(i) for i in line_split[2].split(",")]
    in_file = line_split[0]
    out_file = line_split[1]
    file_info.append([in_file, out_file, date_locations, pid_index])

If something occurs I want Python to proceed printing its usual error messages, but I want to add an additional line at the end of the regular error message, something like:

except:
    print "line \"{0}\"  might have failed to parse".format(line.rstrip())

However, with the code above only the extra info line is shown – the regular error messages are overwritten!

I have tried adding the following to my catch, but it produces ugly output:

e = sys.exc_info()
for i in e:
    print i

Is there a simple way to have Python print the regular error messages plus an additional line of info of my choosing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:47:33+00:00Added an answer on June 15, 2026 at 5:47 am

    I think the best option here (and I removed my other solution in this edit, as I feel it’s actually not a great one) is to create your own exception that describes your problem, and then use it atop the exception you have:

    class ParseFailureError(Exception):
        def __init__(self, line):
            self.line = line.rstrip()
    
        def __str__(self):
            return "line \"{0}\"  might have failed to parse".format(self.line)
    

    then:

    try:
        ...
    except SomeException as exception:
        raise ParseFailureError(line) from exception
    

    Which will produce something like:

    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    NameError: name 'x' is not defined
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "<stdin>", line 4, in <module>
    __main__.ParseFailureException: line "blah" might have failed to parse
    

    (I used a NameError as SomeError for the example)

    Note that I have added a specific exception to your except line – this is always worth doing, as catching any exception could cause you to ignore errors you don’t mean to.

    This method has the advantage of making your code easier to use by other software, as they can catch this particular exception.

    The from syntax on an exception tells Python the root cause of the exception. Otherwise, Python will assume the new exception was an error occurring during the handling of the exception. Note this is only available in Python 3.x, in earlier versions, you will have to do this manually. I would suggest using traceback.format_exc() then printing that as part of your exception’s error message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some C code to parse a text file, first line by line
I have a simple but huge xml file like below. I want to parse
I have some C code that parses a file and generates another file of
I have a quad core machine and would like to write some code to
I am writing some code to parse an xml file of the following format
I've written some basic code to parse a CSV file and input the data
I have some code that I parse and extract some matrices from. Theses matrices
I currently have the following code to parse a csv file using the standard
Possible Duplicate: Parse a JavaScript file through PHP I have some pretty long string
I have some code to parse an email and find the attachments then store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.