View Single Post
Old Apr 28th, 2008, 9:51 PM   #1
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 53
Rep Power: 1 kewlgeye is on a distinguished road
Try and Catch Statements

Ok, I have this code and I am trying to put try and catch statements in it and I am unable to. I receive a compiler error that says can't have try without catch, and catch without try.

How can I do this?

import java.io.*;

class ModifiedTest
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
        String strInput;

        System.out.println("Do you like Rock Music?:");
        strInput = dataIn.readLine();  //reads data from console

try
{
        
	if ("y".equals(strInput))
        {
            System.out.println("Do you like Marilyn Manson?:");
            strInput = dataIn.readLine();  //reads data from console
            if ("y".equals(strInput))
            {
                System.out.println("You are a hardcore Rocker");
            }
            else if ("n".equals(strInput))
            {
                System.out.println("How could you not like Marilyn Manson?");
            }
            else
            {
                System.out.println("Illegal input: " + strInput);
            }
        }
        else if ("n".equals(strInput))
        {
            System.out.println("Ok, bye rock hater!");
        }
        
	else
	catch (TheException e)
        {
            System.out.println("" + e + " input: " + strInput);
        }

        System.out.println();
        System.out.println("Program Ending!");
	}
    }
}
class TheException extends Exception
{
	public String toString()
	{
		return "Illegal";
	}
}
kewlgeye is offline   Reply With Quote