Nick Castello
Nick Castello

Reputation: 51

How do I Throw an IllegalArgumentException that won't terminate my program?

Ok so I have a method with a switch statement but I left out the rest of the cases because they're not important. In my main method, the operator method is called and passed the parameter "selection" in a while loop until they choose "Q".

When the user enters a negative number, it should throw an exception, print a message, and ignore their input but then loop back to the beginning. When this exception is thrown it terminates the program. Any help would be very much appreciated. Thanks!

public static void operator(String selection) throws IllegalArgumentException{
    Scanner input = new Scanner(System.in);
    double price;
switch(selection){
case "A":
     System.out.println("Enter the price");
        if(input.nextDouble()<0){
            throw new IllegalArgumentException("Price cannot be a negative value");
        }
        else{
            price = input.nextDouble(); 
        }
break;

case"Q":
  System.exit(0);
}
}

Upvotes: 2

Views: 10111

Answers (3)

puhlen
puhlen

Reputation: 8529

You've gotten some good answers already on how to handle the exception.

For your case I don't think an exception is appropriate at all. You should get rid of the exception altogether and just handle the problem input by printing an error message and asking for a new input.

Exceptions are for exceptional situations, they should not be part of the normal execution of your code.

Upvotes: 0

Julian
Julian

Reputation: 344

An IllegalArgumentException inherits from RuntimeException, for it not to stop your program you can just use a simple try{} catch {} but i don't recommend doing that with Runtime Exceptions. If that's the case, create your own Exception inheriting from java.lang.Exception.

You can use try catch here.

Something like this should work:

public static void operator(String selection) {
Scanner input = new Scanner(System.in);
double price;
switch(selection){

case "A":
 System.out.println("Enter the price");
     try {
        if(input.nextDouble()<0) {
            throw new NegativePriceException();
        }
     } catch (NegativePriceException e) {
        System.out.println("The price can't be negative.");
        e.printStackTrace();
     }

    price = input.nextDouble(); 
    break;

case"Q":
  System.exit(0);
}
}

And to make your own Exception class you basically need to inherit from Exception (if you want to use try catch on it) or inherit from RuntimeException (if you want it to stop your program from running), like this:

public class NegativePriceException extends Exception {

  public NegativePriceException() {
     super();
  }
}

Upvotes: 5

Jinu P C
Jinu P C

Reputation: 3216

Java requires that you handle or declare all exceptions. If you are not handling an Exception using a try/catch block then it must be declared in the method's signature.

In your main method you should handle the Exception

public static void main(String args[]) {
//codes
try{
operator("A");
}
catch(IllegalArgumentException e){
e.printStackTrace();
}
}

Upvotes: 1

Related Questions