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 6539433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:51:32+00:00 2026-05-25T10:51:32+00:00

I have a number of functions that parse data from files, usually returning a

  • 0

I have a number of functions that parse data from files, usually returning a list of results.

If I encounter a dodgy line in the file, I want to soldier on and process the valid lines, and return them. But I also want to report the error to the calling function. The reason I want to report it is so that the calling function can notify the user that the file needs looking at. I don’t want to start doing GUI things in the parse function, as that seems to be a big violation of separation of concerns. The parse function does not have access to the console I’m writing error messages to anyway.

This leaves me wanting to return the successful data, but also raise an exception because of the error, which clearly I can’t do.

Consider this code:

try:
    parseResult = parse(myFile)
except MyErrorClass, e:
    HandleErrorsSomehow(str(e))

def parse(file): #file is a list of lines from an actual file
    err = False
    result = []

    for lines in file:
        processedLine = Process(line)
        if not processedLine:
            err = True
        else
            result.append(processedLine)
    return result
    if err:
        raise MyErrorClass("Something went wrong")

Obviously the last three lines make no sense, but I can’t figure out a nice way to do this. I guess I could do return (err, result), and call it like

parseErr, parseResult = parse(file)
if parseErr:
    HandleErrorsSomehow()

But returning error codes seems un-pythonic enough, let alone returning tuples of error codes and actual result values.

The fact that I feel like I want to do something so strange in an application that shouldn’t really be terribly complicated, is making me think I’m probably doing something wrong. Is there a better solution to this problem? Or is there some way that I can use finally to return a value and raise an exception at the same time?

  • 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-05-25T10:51:32+00:00Added an answer on May 25, 2026 at 10:51 am

    Nobody says the only valid way to treat an “error” is to throw an exception.

    In your design the caller wants two pieces of information: (1) the valid data, (2) whether an error occurred (and probably something about what went wrong where, so it can format a useful error message). That is a completely valid and above-ground case for returning a pair of values.

    An alternative design would be to pass a mutable collection down to the function as a parameter and let it fill any error messages it wants to emit into that. That will often simplify the plumbing in the caller, especially if there are several layers of calls between the parser and the code that knows how to do something with the error messages afterwards.

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

Sidebar

Related Questions

I have written a function that gets a given number of random records from
We have scalar functions in our database for returning things like number of tasks
I have the following data that basically I only need a few bits of
I have a query in linqtosql that returns a LabelNumber: var q = from
Let's say I have to read from a directory that has many large XML
I have a function that expects real numbers (either integers or floats) as its
I have the following Oracle function: function get_job_no return number is V_job_no number; begin
Say I have a C function which takes a variable number of arguments: How
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
I'm working on a project were we have number (5 at the moment) of

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.