-1

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Also, I would like to add that for this kind of exception you do not want to extend Exception, you want to extend RuntimeException that way it doesn't have to be wrapped with a try-catch every time it's used.
"You catching exception does not mean anything if you just print out the message" <-- I wish IDE template designers had remembers that before auto-generating e.printStackTrace()s in catch blocks...
@fge I wish compilers generate warnings.. IDEs are bogus anyway :)
how to make this precautions. Can you give me a simple example ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.