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

  • Home
  • SEARCH
  • 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 8044397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:12:58+00:00 2026-06-05T05:12:58+00:00

So I noticed that Django has a nice message framework. It has 5 different

  • 0

So I noticed that Django has a nice message framework. It has 5 different levels (info, error, debug, warning, and success).

It would be really nice to propagate the exception all the way up to the views level, and report some of these exceptions.

lib.py

def read_file(user, filename, **kwargs):
   try:
     with open(...):
        return f.read()
   except Exception, e:
     raise e

utli.py

def wrapper_read_file(user, filename, **kwargs):
   try:
      if user.is_authenticated and user.belongs('admin'):
          lib.read_file(...)
      else:
          raise Exception("Unauthenticated user!!!")
   except Exception, e:
      raise e

views.py

def my_view(request):
   [..] do something here
   try:
     result = wrapper_read_file(...)
     return render(request, 'hello.html', {'result': result})
   except Exception, e:
     if isinstance(e, IOError):
         if e.errno==errno.ENOENT:
         messages.add_message(request, message.ERROR, 'File does not exist.')
     elif isinstance(e, OSError):
         messages.add_message(request, message.ERROR, 'You don't have sufficient permission to open the file.')
     return render(request, 'hello.html', {'hello_world': 'hello_world'}

Django knows how to render the messages and I have the facility to do that. So it will display messages.

Do you think my exception handling looks reasonable? Any alternative suggestions? I am pretty new to Python error exception handling.

  • 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-05T05:12:59+00:00Added an answer on June 5, 2026 at 5:12 am

    You probably don’t want to catch every exception. This may mask other errors, and will do things like prevent Ctrl-C from working. Instead, catch only the exceptions you want to handle.

    try:
        # action that may throw an exception
    except IOError, e: # Will only catch IOErrors
        if e.errno == errno.ENOENT:
            messages.add_message(request, messages.ERROR, "File not found")
        else:
            raise e    # Re-raise other IOErrors
    except OSError, e: # Will only catch OSErrors
        messages.add_message(request, messages.ERROR, "Insufficient permissions")
    
    return render(request, 'hello.html', {})
    

    Update: added a second except clause to handle other another exception type. Note that this is still probably insufficient, as this makes the (big) assumption that all OSErrors are permissions related.

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

Sidebar

Related Questions

I've noticed that after I log in with Django registration it redirects me to
I looked through the Django built-in Auth app and noticed that views are split
I am trying to customize Django's comments form. Inside django.contrib.comments.forms I noticed that all
I'm using the i18n feature of Django. I have noticed that even though I
I noticed in the main Django introductin they show a feature that maps python
I noticed that in Django models, there is a class Meta which makes some
I was setting up Django on Cherokee today (using SCGI), and I noticed that
I was looking at some Django examples and I've noticed that you can call
I'm preparing to deploy my Django app and I noticed that when I change
I have a nice and lovely Django site up and running, but have noticed

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.