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 4080842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:58:57+00:00 2026-05-20T17:58:57+00:00

I have a class for threads (implements runnable). I want to be able to

  • 0

I have a class for threads (implements runnable). I want to be able to print text to a file inside the method “run”, but I can’t add “throws IOException” because run() is an implementation of a method in runnable.

Of course there’s no way I’ll use a gazillion “Try-Catch” in run()…

Thanks

  • 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-20T17:58:57+00:00Added an answer on May 20, 2026 at 5:58 pm

    Dealing with checked exceptions in your run() method is tricky for the following reasons:

    1. As @kenson john says (and you observed) you can’t simply let them propagate, because run() is not declared as throwing checked exceptions.

    2. If you catch them and rethrow them wrapped in unchecked exceptions, they are likely to go unnoticed by the main thread. Why? Because the exceptions are thrown on the stack of the child thread. Unless they are caught in the run() method, they will be dealt with by the thread’s UncaughtExceptionHandler object … which is likely to be the default handler … which writes a stacktrace to System.err and then discards the exception.

    3. Now you can set another UncaughtExceptionHandler object for the child thread, or even set a default UncaughtExceptionHandler for the JVM. But what should the handler do? Ideally, the exception needs to be reported back to the main thread (or whatever) to take executive action. But there’s no way that the child thread, or a handler for that thread can throw an exception on the main thread’s stack. (The best you could do to get the main thread’s attention would be to set a flag or call Thread.interrupt() on the main thread … and hope that it checks the flag / interrupt status periodically.)

    4. Any IOException that is thrown while writing to an output file indicates that something serious has gone wrong; e.g. the file system is full, or the socket or pipe you were writing to is broken / closed. So simply ignoring it could be a big mistake. (For instance, suppose that the file is precious, and that the last thing that the application does is to delete the old version of the file and replace it with the new version we just created. Ooops! We didn’t notice that the FS filled up, the new version of the file is corrupt and we just deleted the old version.)

    In the light of this, the PrintStream approach is simplest. A PrintStream will quietly catch any IOException that occurs while writing, but record the fact that an exception has occurred. So when the main thread decides that everything should have finished writing, it needs to call PrintStream.checkError() to test if any errors have occurred. (Unfortunately, the API does not allow you to find out what the actual exception was.)

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

Sidebar

Related Questions

I have a class,which implements Runnable interface. I want to create multiple threads for
Let's say I have a static class with a static method. Multiple threads can
I have a class ( class A for example) implements Runnable . In run
I have a class that implements Runnable. For logging reasons I want to know
I have a thread implemented inside a class using runnable like this: static Runnable
I have a java object that implements Runnable. here is the code. public class
I have a singleton class that is shared by some threads. within a method
I have a java code like this: private class Uploader implements Runnable { //
I have multiple runnable classes and I want to run them with executor service
I have a custom thread pool class, that creates some threads that each wait

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.