Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to control the output of the console?

Reply
Thread Tools

How to control the output of the console?

 
 
Gordon Beaton
Guest
Posts: n/a
 
      05-07-2008
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

--
 
Reply With Quote
 
 
 
 
JTL.zheng
Guest
Posts: n/a
 
      05-07-2008
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"
 
Reply With Quote
 
 
 
 
Dave Miller
Guest
Posts: n/a
 
      05-07-2008
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/
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      05-07-2008
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/>
 
Reply With Quote
 
JTL.zheng
Guest
Posts: n/a
 
      05-07-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to control position of output values on output console pratap C++ 3 07-19-2007 02:38 PM
output text in control location; calling control javascript from page javascript Lisa Calla ASP .Net Building Controls 0 11-09-2004 02:17 AM
Is Fuji S3000 3.2m/pixel output, or 6 m/pixel interpolated output? Peter H Digital Photography 43 12-04-2003 02:35 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments