Try Catch Java

mossman

Gawd
Joined
Feb 10, 2002
Messages
664
try
{
i= Integer.parseInt( iVal );
}

catch(NumberFormatException e)
{
//JOption error message
}


Is this the correct code? i is an int that equals an input screen. The user must enter 1,2,3,4 or 5. I need to just show an error message if the user enters say a "wjfjf" or anything other than the correct numbers. Is this right? Thanks


...and what does that magical "e" mean? I compiled without that e and got like 7 errors. With it I got none.
 
Read about Handling Errors with Exceptions 1st.

The code is correct. If ival is a non-digit value, an exception will be thrown. In your catch clause, it will take an NumberFormatException error, if that type of exception is thrown. the e variable is an instance of NumberFormatException. So you can see the error message and the stack trace and all that good stuff.
 
I don't understand.

I compiled without any errors but the program still crashes.
Code:
import javax.swing.JOptionPane;

public class Menu1 
{
	
	public static void main( String args[] )
   	{
		
		int i;
		
		String iVal;
		
		do
		{
		
			iVal = JOptionPane.showInputDialog("Please Choose Your Option:\n"+ 
				   "1 = Instruction\n"+"2 = Java Quiz\n"+"3 = Current Quiz"+
				   " Score\n"+"4 = Reset Score\n"+"5 = Exit");
		
			i = Integer.parseInt( iVal );
			
		
			try
            {
                i = Integer.parseInt( iVal );
              	
            }
                
            catch ( NumberFormatException e)
            {
                JOptionPane.showMessageDialog( null, "WRONG INPUT", 
                "SORRY", JOptionPane.ERROR_MESSAGE);
            }
			
			switch ( i ) 
	  		{
			
         		
         		case 1:
         		{  
            		JOptionPane.showMessageDialog( null,"You Picked 1","1",
            		JOptionPane.INFORMATION_MESSAGE);
            		break;
            	}
            
            	case 2:
            	{
            		JOptionPane.showMessageDialog( null,"You Picked 2","2",
            		JOptionPane.INFORMATION_MESSAGE);
            		break;
            	
           	 	}		
            
            	case 3:
            	{
            		JOptionPane.showMessageDialog( null,"You Picked 3","3",
            		JOptionPane.INFORMATION_MESSAGE);
            		break;
            	
            	}		
            
            	case 4:
            	{
            		JOptionPane.showMessageDialog( null,"You Picked 4","4",
            		JOptionPane.INFORMATION_MESSAGE);
            		break;
            	
            	}
            	
            	case 5:
            	{
            		JOptionPane.showMessageDialog( null,"Goodbye","Exit",
            		JOptionPane.INFORMATION_MESSAGE);
            		break;
            		
            	}
            	
            	
            	
            	
      		}//end switch 
      		
      		
	
  		}while ( i != 5);
  		
  		JOptionPane.showMessageDialog( null,"You have to pick"+
  		" 5 to get here","!",JOptionPane.INFORMATION_MESSAGE);										
   	}
   
   	
}
 
One problem with that code is you report the Exception but still switch on i, even if they had not enetered a valid integer.
 
Code:
catch ( NumberFormatException e)
{
   JOptionPane.showMessageDialog( null, "WRONG INPUT", 
   "SORRY, USING DEFAULT VALUE 5 (QUIT)", JOptionPane.ERROR_MESSAGE);
   i = 5;
}

Give that a go :D
 
probably be better to have that try/catch inside of a loop...

maybe set i=-1 before the loop and do

while(i != -1){
read input
try{ i = parse_number()}
catch { send error message }
}
 
Also remove the "i= Integer.parseInt(iVal)" that you have before the try block.(Line19)
 
Originally posted by Elmo187
Also remove the "i= Integer.parseInt(iVal)" that you have before the try block.(Line19)

this is the biggest problem with the code
 
Back
Top