开发者

I need to provide to separate exception statements. 1. empty string and 2. valid numeric data

try{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println(" Enter the Amount of articles to be ordered.");
            amount = reader.readLine();

            if(amount.trim().isEmpty()){
                System.out.println("Amount Entered is Empty");
            }

            for(int count=0;count<amount.length();count++){
                if(!Character.isDigit(amount.charAt(count))){
                    throw new NumberFormatException();
                }
            }            
            order.validateAmount(amount);
        }catch(NumberFormatException 开发者_如何学运维numbere){
            System.out.println("Either Number format is uncorrect or String is Empty, Try Again.");
    }

The above code gives me single println() statement for both empty string exception and invalid numeric data exception, which I don't want. I want separate println() statements for both exception. how to get?


  1. You could either use two different exceptions, for instance NumberFormatException and IllegalArgumentException and do two different catch-clauses.

        ...
        if (amount.isEmpty())
            throw new IllegalArgumentException();
        ...
    
    } catch (NumberFormatException numbere) {
        System.out.println("Either Number format is uncorrect, Try Again.");
    } catch (IllegalArgumentException empty) {
        System.out.println("String is empty, Try Again.");
    }
    
  2. Use the same exception but with different messages:

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.println(" Enter the Amount of articles to be ordered.");
        String amount = reader.readLine();
    
        if (amount.trim().isEmpty()) {
            System.out.println("Amount Entered is Empty");
        }
    
        if (amount.isEmpty())
            throw new IllegalArgumentException("String is empty.");
    
    
        for (int count = 0; count < amount.length(); count++)
            if (!Character.isDigit(amount.charAt(count)))
                throw new IllegalArgumentException("Number format incorrect.");
    
        order.validateAmount(amount);
    } catch (IllegalArgumentException e) {
        System.out.println(e.getMessage() + " Try again.");
    }
    
  3. Or, you could roll your own Exception with two different constructors, and a flag stating if the exception was due to a bad number or an empty string.


Since an empty string is an 'expected' exception I would not use an exception but check for it:

if ( amount.trim().equals( string.empty) )
{
   System.out.println("string empty" );
}
else
{
   //do your other processing here
}

Another check for emptiness would be amount.trim().length == 0

If you really want to use exceptions:

if( amount.trim().equals( string.empty) )
{
   throw new IllegalArgumentException( "Amount is not given" );
}

and add another catch()

}
catch( NumberFormatException numbere)
{
}
catch( IllegalArgumentException x )
{
  // Amount not given
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜