Results 1 to 2 of 2

Thread: try/catch problem yet again

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Question try/catch problem yet again

    I've almost got this... I hope.

    Okay here is the peice of code I have a question about:

    Code:
    			while (tryAgain)
    			{
    				try
    				{
    					orderTotal = Double.parseDouble(inputString);
    					while (orderTotal == 0)
    					{
    						inputString = JOptionPane.showInputDialog("Invalid order total \n" + "Please enter a correct order total: ");
    						orderTotal = Double.parseDouble(inputString);
    					}
    					tryAgain = false;
    				}
    				catch (NumberFormatException e)
    				{
    					inputString = JOptionPane.showInputDialog("Invalid order amount. \n" + "Please enter a valid amount: ");
    					orderTotal = Double.parseDouble(inputString);
    				}
    				catch (NullPointerException e)
    				{
    					inputString = JOptionPane.showInputDialog("Invalid order amount. \n" + "Please enter a valid amount: ");
    					orderTotal = Double.parseDouble(inputString);
    				}
    			}
    tryAgain is set to true BTW.

    Without the second catch for the NullPointerException, it keeps looping until an appropriate value is entered. However with the second catch, it doesn't loop. If you press the Cancel button twice, it exits the program. Why is it doing this?
    Last edited by GamerMax5; Nov 17th, 2005 at 04:46 PM.
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: try/catch problem yet again

    your code doesn't make any sense.
    the same code that fires the exception exists inside the exception handler, so if it happened again java will catch it, not your prog, that's why it exits
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width