Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Throw Error - simple code.....any advice appreciated!

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Throw Error - simple code.....any advice appreciated!

    I'm a Newbie to Java - am trying to explore the relationship between objects / classes & have run into a roadblock which I'm sure will seem retarded to folks with more experience. I wrote a very simple program below to call another method & print text 10 times after the user inputs 'Y'. I know its not necessary to put the loop in another class but I'm doing it to develop my understanding however when I compile it I get an error msg: 'Syntax error on token "throws", throw expected'.
    I've declared the exception at the start so not sure why I'm getting this...please help I'd appreciate any info!

    class MyTest {
    public static void main(String args[]) {
    throws java.io.IOException; {
    char ch, y = 'Y';
    OutputIncrements input = new OutputIncrements();
    System.out.println("Enter Y or N");
    ch = (char) System.in.read(); // get a char
    System.out.println("You entered: " + ch);
    if(ch == y) input.range();
    }
    }
    class OutputIncrements {
    void range() {
    for(int i=0; i<10; i++) {
    System.out.println("This loops 10 times");
    if(i == 5) System.out.println("This is the 5th time");
    }
    }
    }
    }


  2. #2
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Throw Error - simple code.....any advice appreciated!

    Hi,

    Please post your code properly with in hightlight tags.

    In Your code mentioned throws IOException in a wrong place. you can put like this,
    public static void main(String args[]) throws java.io.IOException 
    {

    i hope it helps to you.

    Thanks,

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Throw Error - simple code.....any advice appreciated!

    Welcome to the forum. Please read This to know how to post your code.
    the problem is lies here...
    public static void main(String args[]) {
    throws java.io.IOException; {
    why "throws java.io.IOException;" is inside the method body, remove the braces before throws and your "throws java.io.IOException;" also contain the semicolon remove that too.
    syntax for throws method is like
    public void methodName() throws Exception_Class{
    //code
    }
    by using the throws clause you are delegating the exception handling to the calling method.
    for more information on throws read This

  4. #4
    Junior Member
    Join Date
    May 2014
    Posts
    16
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Throw Error - simple code.....any advice appreciated!

    Hey, welcome to the forum, I hope you enjoy it here

    First I would suggest you learn to comment code correctly. You enclose it with [_code]Code Goes Here[/code_] just without the underscores. It just makes it easier for someone to read your code.

    Secondly, I would suggest reading about exception handling. I'm a beginner myself, but if I were you, I would use a try-throw-catch sequence rather than trying to use throws.

    For example:

    try{ //Tries this code
        if (ch.equalsIgnoreCase("y") //If ch is equal to "y"(ignoring lower case)
        {
           range();   //Execute range(). I wouldn't recommend having this function in a seperate Class as then you would need
                         //to create an instance of MyTest (MyTest testResults = new MyTest();) and then say testResults.range();
        }
        else //if (!ch.equalsIgnoreCase("y") //Otherwise if ch is not equal to "y", throw an IOException
        {
            throw new IOException("You chose No.");
        }
    }
    catch(IOException e){ //This is where you catch the IOException (e is the standard identifier for an Exception but any legal identifier will work)
        System.out.println(e.getMessage()); //Prints out the message we defined in the try block.
    }

    There might be a few errors with my syntax. But I hope this helps

    EDIT: Looks like some people have already beaten me to it. And their solution looks a lot simpler haha
    Last edited by MrPigeon; June 6th, 2014 at 01:01 AM.

  5. #5
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Lightbulb Re: Throw Error - simple code.....any advice appreciated!

    Welcome Mr ziggy. Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.


    As for your code, this is the problem:

     public static void main(String args[]) {
     throws java.io.IOException; {

    You should remove the bracket after (String args[]) and also remove the semicolon ( ; ) after throws java.io.IOException

    It should look like this:

     public static void main(String args[])
     throws java.io.IOException {

    also remove one curly bracket at the end to balance the no. of curly crackets.

    Now it should work.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Throw Error - simple code.....any advice appreciated!

    That's perfect thanks for all your help guys.

    Will post code in highlight tags in future, Cheers!

Similar Threads

  1. [SOLVED] Error in absurdly simple code
    By Helplessdrowningpuppy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2014, 10:42 PM
  2. [SOLVED] If input value is wrong, throw error and reenter
    By sabbath in forum Loops & Control Statements
    Replies: 4
    Last Post: February 18th, 2013, 02:14 PM
  3. My code have a simple error but i dont know how to solve it !!!
    By Blackhawk1993 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 17th, 2011, 01:12 PM
  4. Calculator Throw Error
    By bengregg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 5th, 2011, 09:25 PM
  5. Simple code error
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2010, 03:25 PM