Results 1 to 12 of 12
  1. #1
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default what is "standard error stream"

    Could anyone give me some code , displaying
    how "stderr" works in java.
    Thanks

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    As in?

    STDERR is the standard channel to which error messages are printed and, in most cases, is simply "the console" and the same as STDOUT which is where non-error messages get printed, which is also generally "the console". Now what do you actually want?

  3. #3
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Thanks a lot.
    I want to print out the error messages with the line number that the error occurred.
    Can you please tell me if there is any method that diplays such LINE , or any piece of code that prints out such error message with line.

    Thanks again.
    Ted

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Java Code:
    public class A {
    	public static void main(String[] args) {
    		A a = new A();
    		a.start();
    	}
    
    	private void start() {
    		String s = null;
    		try {
    			s.length();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}	
    	}
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    Is there a way of getting the stack trace without having a Throwable?
    Otherwise you would need to do something like:
    Java Code:
    System.out.println(new Throwable().getStackTrace()[0].toString());
    which seems a bit of overkill to me.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Is there a way of getting the stack trace without having a Throwable?
    Otherwise you would need to do something like:
    Java Code:
    System.out.println(new Throwable().getStackTrace()[0].toString());
    which seems a bit of overkill to me.
    A Thread can produce a stacktrace.

    Row, row, row you boat gently down the error stream ... <--- on topic.

    kind regards,

    Jos

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    To state bluntly what they've hinted at - almost the only thing that prints out lines is Throwable#printStackTrace(). It traces the lines (and methods) that the Throwable propagated through to either reach the top level (either the main method of a thread's run method) or a catch block.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Not quite.
    Java Code:
    Thread.currentThread().getStackTrace()
    see the API docs. This, I believe, is what Jos was getting at.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by masijade View Post
    Not quite.
    Java Code:
    Thread.currentThread().getStackTrace()
    see the API docs. This, I believe, is what Jos was getting at.
    Yep, that's what I was hinting at.

    Merrily, merrily, merrily, merrily, /dev/null is but a dream <--- on topic

    kind regards,

    Jos

  10. #10
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Really appreciated of your attention and time. Thanks.
    But I have an interesting question here.
    What if error occurred in a file, when reading text from it?
    E.g. be it math (2/0), mismatching tags etc.
    Code:
    BufferedReader in = new BufferedReader(new FileReader("filename"));
    String str;
    while ((str = in.readLine()) != null) {
    System.out.println(str);
    ..............(working on the streams here).................
    }

    Could it be possible to show the error with line numbers?
    Pleaseeeeee

    Cylab

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    you would need to count the number of lines read in your code, but it should work just fine.

    i.e. (adjusting the code you posted - and please use code tags [code] and [/code] around code you post)
    Java Code:
    int lines=0;
    while((str=in.readLine())!=null){
         ++lines;
         if(isError()/*an error occurred*/){
              System.err.println("An error occurred at line " + lines);
         }else{
              //do regular processing
         }
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  12. #12
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Thanks Singing Boyo.
    your code is really helpful, though I`ve never
    seen the isError() method.

    Thanks again

    Cylab

Similar Threads

  1. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. "parse error at or before "}""
    By constden in forum New To Java
    Replies: 32
    Last Post: 04-25-2009, 09:49 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •