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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:18:34+00:00 2026-05-23T13:18:34+00:00

This pertains to urllib2 specifically, but custom exception handling more generally. How do I

  • 0

This pertains to urllib2 specifically, but custom exception handling more generally. How do I pass additional information to a calling function in another module via a raised exception? I’m assuming I would re-raise using a custom exception class, but I’m not sure of the technical details.

Rather than pollute the sample code with what I’ve tried and failed, I’ll simply present it as a mostly blank slate. My end goal is for the last line in the sample to work.

#mymod.py
import urllib2

def openurl():
    req = urllib2.Request("http://duznotexist.com/")
    response = urllib2.urlopen(req)

#main.py
import urllib2
import mymod

try:
    mymod.openurl()
except urllib2.URLError as e:
    #how do I do this?
    print "Website (%s) could not be reached due to %s" % (e.url, e.reason)
  • 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-23T13:18:34+00:00Added an answer on May 23, 2026 at 1:18 pm

    You can add information to and then re-raise the exception.

    #mymod.py
    import urllib2
    
    def openurl():
        req = urllib2.Request("http://duznotexist.com/")
        try:
            response = urllib2.urlopen(req)
        except urllib2.URLError as e:
            # add URL and reason to the exception object
            e.url = "http://duznotexist.com/"
            e.reason = "URL does not exist"
            raise e # re-raise the exception, so the calling function can catch it
    
    #main.py
    import urllib2
    import mymod
    
    try:
        mymod.openurl()
    except urllib2.URLError as e:
        print "Website (%s) could not be reached due to %s" % (e.url, e.reason)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question pertains specifically to shell scripts, but could be about any programming language.
This pertains to C# & the .NET Framework specifically. It's best practice not to
Not sure whether this pertains to VS 2010 or to the upgraded framework, but...
[This questions pertains to Access 2003] I need to add an additional criteria (b)
I'm using ASP.NET, but I'm sure this pertains to any (or most) MVC frameworks.
This question pertains not only to how to accomplish this task, but to whether
This question is similar, but it pertains to static methods: In Python, how do
This pertains to Linux kernel 2.6 TCP sockets. I am sending a large amount
I hardly see any pointer on the following problem related to Hibernate. This pertains
This problem pertains to Java By using RandomAccessFile I intend to be able to

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.