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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:36:39+00:00 2026-05-31T19:36:39+00:00

What is the best practice to throw an ArgumentTypeError exception from my own custom

  • 0

What is the best practice to throw an ArgumentTypeError exception from my own custom action and let the argparse to catch it for me?

It seems that argparse’s try/except block does not handle this exception for my custom actions. Though it does that just fine for its built-in actions.

class unique_server_endpoints(argparse.Action):
    """This class avoids having two duplicate OuterIPs in the client argument list"""
    def __call__(self, parser, namespace, values, option_string=None):
        ips = set()
        endpoints = []
        for r in values:
            ep = server_endpoint(r)
            if ep[0] in ips:
                raise argparse.ArgumentTypeError("Duplicate OuterIPs found")
            else:
                ips.add(ep[0])
                endpoints.append(ep)
        setattr(namespace, self.dest, endpoints)

and

group.add_argument('-c', "--client", nargs = 2,
            dest = "servers", action = unique_server_endpoints,

For example, in the code above If I would have duplicate IPs then the exception would fall down to the main function and print the ugly stacktrace. I don’t want that and neither I don’t want to put a try/except block inside main.

  • 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-31T19:36:41+00:00Added an answer on May 31, 2026 at 7:36 pm

    After looking at argparse source code I figured out that it translates ArgumentTypeError to ArgumentError exception.

    So instead of:

                raise argparse.ArgumentTypeError("Duplicate OuterIPs found")
    

    I should have:

                raise argparse.ArgumentError(self, "Duplicate OuterIPs found")
    

    And argparse would still do the rest for me (catch exception and print usage message) …

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

Sidebar

Related Questions

The best practice seems to be to use assert for a condition that should
Is it best practice to wrap a web service method/call into a try/catch block?
What is the best practice to follow when you need to throw an exception
Best practice to throw the exception if no entry found in the db? //
Is there a best-practice or industry standard for throwing exceptions from a toolkit API?
What is the standard (or best practice) for Groovy error messages that that shouldn't
What is the best practice when returning data from functions. Is it better to
What's the best practice for retrieving JSON values that may not even exist in
I was hoping that someone could tell me the best practice for the scenario
What is the best practice for throwing an Exception in the following situation: My

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.