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 4 of 4

Thread: error in declaration of try statement.

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post error in declaration of try statement.

    please help me to remove the error coming with thy and catch statement in 8 and 17 line

    import java.io.*;

    class UserThread extends Thread
    {
    static int a[]=new int[10];
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    try
    {
    System.out.println("User thread started");
    for(int i=0;i<10;i++)
    {
    System.out.println("Enter "+(i+1)+" value");
    a[i]=Integer.parseInt(br.readLine());
    }
    }
    catch(InterruptedException e)
    {
    System.out.println("Tread is about to interrupt");
    }
    }
    class InterruptTest
    {
    public static void main(String s[])
    {
    UserThread th=new UserThread();
    th.start();

    System.out.println("main thread is about to sleep");
    try
    {
    sleep(20000);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    th.interrupt();
    int sum=0;
    for(int i=0;i<10;i++)
    {
    sum+=UserThread.a[i];
    }
    System.out.println("Sum= "+sum);
    }
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    571
    My Mood
    Sleepy
    Thanks
    9
    Thanked 84 Times in 78 Posts

    Default Re: error in declaration of try statement.

    Wrap your code in code tags and post the exact error message you get.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,482
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: error in declaration of try statement.

    Please, please, please use code tags.

    The try/catch clause in class UserThread exists outside a method. Whaddya thinkin'?

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: error in declaration of try statement.

    Edward,
    Your program has so many syntax errors.
    First of all you did not wrap your thread code in the run() method? I don't see run().
    Secondly you cannot catch InterruptedException when it is not thrown. Instead you may have to catch IOException.
    Can you please fix that and let us know if the problem still persists.
    Lastly, as a guideline it is always better to indent the code so that we know easily where a block starts and ends.

    Thanks,
    Syed.

Similar Threads

  1. Error: Invalid method declaration; return type required. Related to JFrames.
    By Pakupakuman in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 26th, 2012, 05:47 PM
  2. Not A Statement - Java Error
    By shagil.a.gopinath in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 22nd, 2012, 04:54 PM
  3. error: invalid meyhod declaration
    By iswan in forum AWT / Java Swing
    Replies: 0
    Last Post: September 30th, 2011, 08:03 PM
  4. Methods Help - Unreachable statement error
    By jessica202 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2011, 10:24 AM
  5. Use variable in array declaration statement
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2010, 03:42 PM