Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC