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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:24:02+00:00 2026-06-03T00:24:02+00:00

I am using args4j to parse the arguments given to my program. Here is

  • 0

I am using args4j to parse the arguments given to my program.

Here is the code where I define 2 arguments of Date type. The handler just parses the given date and throws a CommandLineException if the date is malformed.

@Option(name="-b", metaVar="<beginDate>", handler=DateOptionHandler.class, usage="...")
private Date beginDate;

@Option(name="-e", metaVar="<endDate>", handler=DateOptionHandler.class, usage="...")
private Date endDate;

I need to be able to return a different code (int value) if it is beginDate or endDate which throws an exception.

Currently, my main method looks like this :

CmdLineParser parser = new CmdLineParser(this);
parser.setUsageWidth(120);

try {
    parser.parseArgument(args);
} catch (CmdLineException e) {
    /* Print usage if an error occurs during the parsing */
    System.err.println(e.getMessage());
    System.err.println("Usage : java LaunchProgram [options]");
    e.getParser().printUsage(System.err);

    /* What I need to do : */
    if(optionWhichThrewTheException.equals("-b") return 2;
    if(optionWhichThrewTheException.equals("-e") return 3;

    /* Other arguments */
    return -1;
}

But I can’t figure out how I can know which argument threw the exception (I looked the CmdLineException methods, but I found nothing).

Is there a way to obtain the parameter which can not be parsed ?

Thanks by advance for your help.

  • 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-03T00:24:03+00:00Added an answer on June 3, 2026 at 12:24 am

    I’ve never used args4j, but looking at its documentation, it seems that the exception is thrown by the option handler. So, use a BDateOptionHandler and a EDateOptionHandler, that throw a custom subclass of CmdLineException containing the needed information:

    public class BDateOptionHandler extends DateOptionHandler {
        @Override
        public int parseArguments(Parameters params) throws CmdLineException {
            try {
                super.parseArguments(params);
            }
            catch (CmdLineException e) {
                throw new ErrorCodeCmdLineException(2);
            }
        }
    }
    
    public class EDateOptionHandler extends DateOptionHandler {
        @Override
        public int parseArguments(Parameters params) throws CmdLineException {
            try {
                super.parseArguments(params);
            }
            catch (CmdLineException e) {
                throw new ErrorCodeCmdLineException(3);
            }
        }
    }
    
    ...
    try {
        parser.parseArgument(args);
    } 
    catch (CmdLineException e) {
        ...
        if (e instanceof ErrorCodeCmdLineException) {
            return ((ErrorCodeCmdLineException) e).getErrorCode();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using LINQ on collections, what is the difference between the following lines of code?
Using the javascript function function squareIt(number) { return number * number; } When given
Using this code, the following execution yields strange results: C 100 R W The
Using Rails 3.2.0.rc2 and ruby 1.9.3p0 In app/views/requests/_form.html.erb I have the following code for
I have an existing java command-line program, that takes a bazillion of arguments and
Using Java, how can I extract all the links from a given web page?
Using R, it is trivial to calculate the quantiles for given probabilities in a
(Using iOS 5 and Xcode 4.2.) I've followed the instructions here: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html#//apple_ref/doc/uid/TP40009497-CH6-SW15 and used
Using DMD 2.057, I cannot get the following code to compile: import std.stdio; import

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.