Q:

Java - include negative values in 'catch' block for Double.parseDouble()

I have a Java client application that can send a message to the server. I am setting up input validation so the client detects whether an acceptable message has been sent on the 'send' button-click.
The messages that should be accepted are anything that can be parsed to a double, or a number of selected strings (recognised by the string.equals() method).
I test for the strings first, and if the condition is not met, I try to parse the message to double (Double.parseDouble(message)). At this point, I have a try/catch block that recognises any failed attept to parse to double (meaning it must contain letters and is therefore invalid), that catches the NumberFormatException and allows the user a chance to re-enter the message.
I would now like to make it so that the number cannot be negative, and would like to include this in the same try/catch block so that any negative value can give the user the same opportunity to re-enter.
Here is what I currently have:
    else
    {   
        try
        {
            //convert number to double (monetary value)
            bidAmount = Double.parseDouble(messageToServer);
            //send to server with item code
            output.println(selectedItemCode + bidAmount);
        }
        //item cannot turned to double
        catch (NumberFormatException numfEx)
        {
            //inform user
            fromServer.setText("Invalid Request!"
                    + USER_PROMPT);
        }
    }
    //clear input field for subsequent entry
    toServer.setText("");

Can anyone point me in the direction of how to implement this rule, without code duplication if at all possible?
Thanks,
Mark

A:

After bidAmount = Double.parseDouble(messageToServer);, we need to add the following:
if(bidAmount < 0){
  throw new NumberFormatException("Invalid amount");
}

An alternate solution (and probably, best practice) would be to modify catch block to catch IllegalArgumentException and throw IllegalArgumentException if the amount is less than 0 as shown below:
try
        {
            //convert number to double (monetary value)
            bidAmount = Double.parseDouble(messageToServer);
            if(bidAmount < 0){
                throw new IllegalArgumentException("Invalid amount");
            }
            //send to server with item code
            output.println(selectedItemCode + bidAmount);
        }
        //item cannot turned to double
        catch (IllegalArgumentException numfEx)
        {
            //inform user
            fromServer.setText("Invalid Request!"
                    + USER_PROMPT);
        }

