Click here to Skip to main content
15,907,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Once I try to compile my program I receive the following errors:

C:\Java 1\New folder (2)\Chapter 04\MyType.java:53: 'catch' without 'try'
				catch(NumberFormatException e)
                                
C:\Java 1\New folder (2)\Chapter 04\MyType.java:23: 'try' without 'catch' or 'finally'
			try


What I have tried:

Java
import java.io.*;
import javax.swing.JOptionPane;

public class MyType
{
	public static void main(String[] args)
	{
		String strChoice = "", strTryString, strTryInt, strTryDouble;
		int choice, tryInt;
		double tryDouble;
		boolean done = false;

		while(!done)
		{
			try // 1st error
			{
				System.out.println("What's my type?");
				strChoice = JOptionPane.showInputDialog(null,"\n\n\n1) String\n2) integer\n3) double\n4) Quit the program");
				choice = Integer.parseInt(strChoice);
				switch(choice)
			    {
					case 1:
					     JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
					     break;

					case 2:
					     JOptionPane.showMessageDialog(null, "Correct!");
					     tryInt = Integer.parseInt(strChoice);
					     break;

					case 3:
					     JOptionPane.showMessageDialog(null, "Correct!");
					     tryDouble = Integer.parseInt(strChoice);
					     break;

					case 4:
					     done = true;
					     JOptionPane.showMessageDialog(null, "Exit.");
					     System.exit(0);
					     break;

					default:
						 throw new NumberFormatException();
				}
				catch(NumberFormatException e) // 2nd error
				{
					JOptionPane.showMessageDialog(null, "Invalid, try again.");
				}

			}
		}
	}
}


// All of the braces are correctly aligned.
Posted
Updated 14-Aug-18 15:51pm
v2

1 solution

Your "catch" block is "inside" your "try" block; s/b

Java
try {
...
} catch (...) {
...
}
 
Share this answer
 
Comments
Member 13652359 15-Aug-18 12:46pm    
Thanks, It worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900