Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 264653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:42:13+00:00 2026-05-11T22:42:13+00:00

Is there some way I can catch when a method generates a write to

  • 0

Is there some way I can catch when a method generates a write to STDERR?
I’m trying to pre-pend some information with each STDERR write.

let’s say our method is:

parser.thatGeneratesSTDERR();

it’ll generate something like

line 37:29 no viable alternative at character ‘a’

can I just wrap it with something that catches the stderr to be written and pre-pend some
info so it’ll look like this instead:

File: mybadfile.txt — line 37:29 no viable alternative at character ‘a’

I know I could probably dig through the actual code that does the STDERR writing but I really don’t want to do that — I’d rather just wrap some code around this method instead.

I should note that this method doesn’t always generate STDERR — the reason I’m trying to catch them is that it SHOULDN’T be generating any STDERR — but since this is called on quite a few files (over 40) I never know which file is generating the error message — it takes forever to find.

UPDATE
so yeh, we can change our STDERR — I was just forgetting that I need to override my println in my new STDERR… here’s the relevant code:

public static String errfile = "";

static {
  final PrintStream currentErr = System.err;
  PrintStream newErr = new PrintStream(currentErr)
  { 
    public void println(String string) {
      print("File: " + errfile + " --");
      print(string);
      super.println();
    }
  };

  System.setErr(newErr);
}

then for each file that I’m testing all I have to do is change errfile to the filename and my output comes out expected…

thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:42:13+00:00Added an answer on May 11, 2026 at 10:42 pm

    I’m not sure if this will work, given how System.setErr works.

    I would create a new class that extends print stream and overwrites the methods to prepend your comment. Then set the error stream to your new printstream. Do not call System.err.printxxx from this new class.

    ex…

    class PrependPrintStream extends PrintStream {
    
    PrependPrintStream(PrintStream ps, String pre){...}
    ...
    public void print(boolean b) {
    super.print(this.pre);
    super.print(b);
    }
    ...
    

    I looked into sun’s PrintStream impl, and it looks like all printxxx delegate to write(String) which is private instead of protected. I guess you can copy/paste the code.

    EDIT::

    A much better solution is to use Logging instead of System.err. This allows you to turn off and on your statements and allows you to create a custom error handler. Your specific handler could prepend the statement you wish to the string before printing to System.err. This also has the advantage that only statements that use your specific logger would have the prepended text and other calls to System.err wouldn’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 200k
  • Answers 200k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, there are publicly available libraries that implement connection pooling… May 12, 2026 at 7:58 pm
  • Editorial Team
    Editorial Team added an answer Without redirection, Luc Vu or Erik Konstantopoulos point out to:… May 12, 2026 at 7:58 pm
  • Editorial Team
    Editorial Team added an answer Why are you getting the product IDs and then using… May 12, 2026 at 7:58 pm

Related Questions

If my application is out of memory when i call new() i will get
I'm looking for an elegant way to suppress exceptions when calling a method. I
The major difference between a thing that might go wrong and a thing that
I have inherited couple of .Net (C#) application which does not have any tracing

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.