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

  • Home
  • SEARCH
  • 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 8974349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:39:45+00:00 2026-06-15T18:39:45+00:00

In java, when we catch a exception, we usually can use printStackTrace() method to

  • 0

In java, when we catch a exception, we usually can use printStackTrace() method to print the error information, and we can also use printStackTrace(PrintStream out) to direct those information to a file.

But how can I append those information into a existing file, just like using out.append()?

  • 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-06-15T18:39:45+00:00Added an answer on June 15, 2026 at 6:39 pm

    You must open file in append mode:

    try {
        //...
    } catch (Exception e) {
        try(Writer w = new FileWriter("file.log", true)) {
            e.printStackTrace(new PrintWriter(new BufferedWriter(w)));
        }
    }
    

    If you are not using Java 7, you must remember about closing or at least flushing the Writer. Or you can have a global Writer, but then you must synchronize it between threads.

    What about simply using some existing Java library like logback, log4j or even java.util.logging? Simply say:

    } catch (Exception e) {
        log.error("Opps!", e);
    }
    

    …and the framework will log the exception wherever you want, with lots of additional data like thread name, timestamp, additional message, etc. Logback can also show you from which library given stack frame comes from and even print the stack trace starting from root cause (most nested exception).

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

Sidebar

Related Questions

Can you try/catch a stack overflow exception in java? It seems to be throwing
Why in Java can we catch an Exception even if it is not thrown,
In Delphi, how can you use try, finally, and catch together? A Java/C# equivalent
Hi guys i catch exception: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 Code look like this:
Why are some exceptions in Java not caught by catch (Exception ex) ? This
Is there some sort of exception in Java to catch an invalid Date object?
Java's checked exceptions sometimes force you to catch a checked exception that you believe
Usually, when dealing with Java IO code, here is what I wrote FileOutputStream out
I usually layout my Java SE methods like this. Should I try to catch
I'm getting this exception: Exception: java.lang.IllegalStateException: Can't copy a recycled bitmap My code is:

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.