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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:09:33+00:00 2026-06-06T00:09:33+00:00

My django view goes through a list , uses regex to detect specific elements

  • 0

My django view goes through a list , uses regex to detect specific elements in the list and finally returns a dict of the contents.
Both IndexError and ValueError can occur while parsing the list.

I need to handle the exceptions in this case.I tried like this

def parse_list(oldlist):
    try:
        newlist=create_newlist(oldlist)
    except Exception as e:
        logger.debug(str(e))
    else:
        mydict = make_dict(newlist)

def create_newlist(oldlist):
    mydict = {}
    for elem in oldlist:
        if re.match('somepattern',elem[0]):
            mydict['somekey']=int(elem[0].strip())
        else:
            raise ValueError(elem[0],' expects an integer')
    ...
    return mydict

Is using the Exception class in except Exception as e: the correct way to handle any exception originating from the above function?

when I wrote a unit test method

def test_me(self):
    dl = parse_list(self.somelist)
    print 'dl=\n',dl
    self.assertTrue(len(dl),0)

I got the console output as

ERROR: test_me (myapp.tests.ParseTest)
..
IndexError: list index out of range

Why is the exception not logged by logger?

  • 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-06T00:09:35+00:00Added an answer on June 6, 2026 at 12:09 am

    Is using the Exception class in except Exception as e: the correct way
    to handle any exception originating from the above function?

    When handling exceptions you should try to be as specific as possible. In your case, you should catch IndexError and ValueError instead of a general Exception:

    try:
        ...
    except (ValueError, IndexError) as e:
        ...
    

    Your other question:

    Why is the exception not logged by logger?

    It depends on the configuration of the logger. You’re printing a ‘debug’ message but it could be set to only log/display messages with level ‘error’ or higher. See logging documentation in Python for more information.

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

Sidebar

Related Questions

I have a django view that returns HTTP 301 on a curl request: grapefruit:~
I have a simple Django view that just returns URL parameters, but if I
I has a Django view which is accessed through an AJAX call. It's a
recently I wanted to return through a Django view a dynamically generated XML tree.
Consider this Django view which will get a list of items associated to the
I have a django view which uses a template to display a long queryset
I have a Django view called change_priority. I'm posting a request to this view
I have 2 forms in my Django view. How can I do a check
I have such stuff in my django view: message = 'sometext' rating = 7
I am sending an AJAX request to a Django view that can potentially take

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.