简体   繁体   中英

Java: Thow new exception for args.length == 0. Doesn't work?

I am trying to throw new exception when args.length doesn't have any arguments supply but instead of printing out my message it prints out ArrayIndexOutOfBounce exception.

 if (args.length == 0)
  {
    throw new DeleteFieldException("You must set at least three arguments");
  }

my catch:

 catch (DeleteFieldException exception)
{
   System.err.println(exception);
} // catch

and my custom made exception class:

public class DeleteFieldException extends Exception
    {
      private String message = null;

      public DeleteFieldException ()
      {
         super ();
      }

      public DeleteFieldException (String message)
      {
        super (message);
        this.message = message;
      }

      public DeleteFieldException (Throwable cause)
      {
        super (cause);
      }

      public DeleteFieldException (String message, Throwable cause)
      {
        super (message, cause);
      }

      public String toString()
      {
        return message;
      }

      public String getMessage()
      {
        return message;
      }

    } // DeleteFieldException

You catching exception does not mean anything if you just print out the message. Your code keeps on executing and probably hits the line where you actually make the array access like argv[n] and it throws this exception, you do not catch it so it crashes.

You have to take actual precautions in your catch block.

Edit for precautions requested:

Those precautions are completely application-specific, but from the code that you supplied I understand that your application takes command line arguments, and by exception handling you are checking whether the user gave enough command line arguments for your code to run.

In that case question you should ask yourself is "What should I do, if the user does not give necessary arguments?"

First possible answer(and most likely), in this example, since you need command line arguments, you have to ask user to re-run the program with correct arguments, and quit peacefully. In that scenario, assuming you do exception handling in the main method you just return to quit. Code is below:

catch (DeleteFieldException exception)
{
   System.err.println("Some error message");
   return;
} // catch

If you are doing this stuff in another method other than main you can return some value that you use to notify the caller method that there is something wrong. If method is supposed to return an Object return null , if it is supposed to return a non-negative number return -1 etc.

Secondly , you can assign some default values for those you expect user to give you, and that catch block is the perfect place to do that. Check out the code below for a slightly different scenario.

try{
    myVariable = argv[1];
}
catch(ArrayIndexOutOfBoundsException e){
    myVariable = 10; //lets say 10 is the default value
}

It doesnt even check argc == N like your code, it simply tries to assign the command line argument to a variable. If user didn't give any input argv[1] causes the exception, you catch it and as precaution you assgin a default value to that variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM