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

Thread: unexpected output

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default unexpected output

    Why doesn't the following code display "stop" ? If i specify the function sleep() without a try/catch, then an interrupted exception is thrown. So now when I've given the try/catch block, that exception should be caught in the catch block and the string "stop" should be printed. But the output isn't so. Can anyone please explain?
    public class A
    {
                     public static void main(String[] args)
                   {
                    Thread t=Thread.currentThread();
                    for(int i=1;i<6;i++)
                        {
     
                         System.out.println( i );
     
                           try
                           {
                            Thread.sleep(1000);
                           }
     
                             catch(Exception e)
                            {
                             System.out.println("stop");
                            }
     
                         }
     
                    }
    }
    Last edited by sumitroy; June 29th, 2014 at 07:47 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unexpected output

    What exception is being thrown that you expect to execute the catch block?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    an interrupted exception is thrown.
    Please copy the full text of the error message and paste it here. It has important info about the error.

    The compiler will give an error message if the code doesn't wrap the sleep() in a try{}catch
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: unexpected output

    the compiler gives the following error when i specify sleep() without try/catch{}:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Unhandled exception type InterruptedException

    at com.home.A.main(A.java:13)

    so definitely an exception should be caught when i specify the try/catch block,but it's not happening.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unexpected output

    Unhandled exception
    The compiler is telling you to wrap some part of the code in a try{}catch block because that code can throw an exception.

    without a try/catch, then an interrupted exception is thrown
    No exception is thrown during compilation. There is a statement that CAN throw an exception when it is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: unexpected output

    thanks!

Similar Threads

  1. Replies: 2
    Last Post: May 12th, 2014, 11:45 AM
  2. unexpected results to simple program
    By Pajaro in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 30th, 2013, 12:51 PM
  3. Unexpected Output Format
    By playinmyblues in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 13th, 2012, 06:56 PM
  4. Unexpected ArrayOutOfBoundsError
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 28th, 2010, 04:00 AM
  5. ArrayList Unexpected Return
    By Cammack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2010, 09:23 PM