How to control the output of the console?

Discussion in 'Java' started by Gordon Beaton, May 7, 2008.

  1. On Wed, 7 May 2008 06:05:47 -0700 (PDT), JTL.zheng wrote:
    > if I run it, the console will output:
    >
    > 1
    > 2
    > 3
    > 4
    > java.lang.Exception
    > at hartech.kids.jim.view_c.Main_C.main(Main_C.java:35)
    >
    > it's not what I expect.
    > what I want of the output order is:
    >
    > 1
    > 2
    > java.lang.Exception
    > at hartech.kids.jim.view_c.Main_C.main(Main_C.java:35)
    > 3
    > 4
    >
    > why the "e.printStackTrace();" output is printed out lastly?
    > can we make it print instantly? just after "2"


    I get the expected output when I run your program "manually" in an
    xterm. How are you running your program exactly? Is this a Netbeans
    thing?

    What happens if you print the stacktrace like this instead:

    e.printStackTrace(System.out);

    or:

    e.printStackTrace(System.err);

    /gordon

    --
     
    Gordon Beaton, May 7, 2008
    #1
    1. Advertising

  2. Gordon Beaton

    JTL.zheng Guest

    like the code:

    try {
    System.out.println(1);
    throw new Exception();
    }catch(Exception e) {
    System.out.println(2);
    e.printStackTrace();
    System.out.println(3);
    }
    System.out.println(4);

    if I run it, the console will output:

    1
    2
    3
    4
    java.lang.Exception
    at hartech.kids.jim.view_c.Main_C.main(Main_C.java:35)

    it's not what I expect.
    what I want of the output order is:

    1
    2
    java.lang.Exception
    at hartech.kids.jim.view_c.Main_C.main(Main_C.java:35)
    3
    4

    why the "e.printStackTrace();" output is printed out lastly?
    can we make it print instantly? just after "2"
     
    JTL.zheng, May 7, 2008
    #2
    1. Advertising

  3. Gordon Beaton

    Dave Miller Guest

    JTL.zheng wrote:
    > like the code:
    >
    > try {
    > System.out.println(1);
    > throw new Exception();
    > }catch(Exception e) {
    > System.out.println(2);
    > e.printStackTrace();
    > System.out.println(3);
    > }
    > System.out.println(4);
    >
    > if I run it, the console will output:
    >
    > 1
    > 2
    > 3
    > 4
    > java.lang.Exception
    > at hartech.kids.jim.view_c.Main_C.main(Main_C.java:35)
    >
    > it's not what I expect.
    > what I want of the output order is:
    >
    > 1
    > 2
    > java.lang.Exception
    > at hartech.kids.jim.view_c.Main_C.main(Main_C.java:35)
    > 3
    > 4
    >
    > why the "e.printStackTrace();" output is printed out lastly?
    > can we make it print instantly? just after "2"


    You may want to check the rest of your code, on my machine it yields:

    1
    2
    java.lang.Exception
    at Tester.main(Tester.java:33)
    3
    4
    --
    Dave Miller
    Java Web Hosting at
    http://www.cheap-jsp-hosting.com/
     
    Dave Miller, May 7, 2008
    #3
  4. Gordon Beaton

    Daniel Pitts Guest

    Dave Miller wrote:
    > JTL.zheng wrote:
    >> like the code:
    >>
    >> try {
    >> System.out.println(1);
    >> throw new Exception();
    >> }catch(Exception e) {
    >> System.out.println(2);
    >> e.printStackTrace();
    >> System.out.println(3);
    >> }
    >> System.out.println(4);
    >>
    >> if I run it, the console will output:
    >>
    >> 1
    >> 2
    >> 3
    >> 4
    >> java.lang.Exception
    >> at hartech.kids.jim.view_c.Main_C.main(Main_C.java:35)
    >>
    >> it's not what I expect.
    >> what I want of the output order is:
    >>
    >> 1
    >> 2
    >> java.lang.Exception
    >> at hartech.kids.jim.view_c.Main_C.main(Main_C.java:35)
    >> 3
    >> 4
    >>
    >> why the "e.printStackTrace();" output is printed out lastly?
    >> can we make it print instantly? just after "2"

    >
    > You may want to check the rest of your code, on my machine it yields:
    >
    > 1
    > 2
    > java.lang.Exception
    > at Tester.main(Tester.java:33)
    > 3
    > 4

    It depends. e.printStackTrace() will default to writign to System.err,
    you can pass in System.out as a parameter, and that should fix your problem.


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, May 7, 2008
    #4
  5. Gordon Beaton

    JTL.zheng Guest

    Thanks very much to all of you.
    I got it now.
    e.printStackTrace(System.out) will help.
    I don't know that e.printStackTrace() is printed to the System.err,
    that why the disorder happens.
     
    JTL.zheng, May 7, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Finney
    Replies:
    2
    Views:
    1,027
    Egor Bolonev
    Jun 30, 2003
  2. Egor Bolonev
    Replies:
    0
    Views:
    500
    Egor Bolonev
    Jun 30, 2003
  3. Serge Terekhoff
    Replies:
    0
    Views:
    661
    Serge Terekhoff
    Jun 30, 2003
  4. pratap
    Replies:
    3
    Views:
    485
    James Kanze
    Jul 19, 2007
  5. Replies:
    5
    Views:
    3,473
    James Kanze
    Mar 4, 2008
Loading...

Share This Page