I currently have the following sample code that I am trying to convert OutputStream to InputStream, which I got the idea from Method 2 in http://blog.ostermiller.org/convert-java-outputstream-inputstream
But my question here is, the save method could throw IOException, and I would like to catch that and re-throw that as part of this getInputStream method.
I am trying to wrap the IOException thrown by save(out) to a runtime exception, but I know that this runtime exception cannot be caught by the parent thread. So I am stuck on this, can anyone point me some directions?
public InputStream getInputStream() throws IOException { PipedInputStream in = new PipedInputStream(); PipedOutputStream out = new PipedOutputStream(in); Thread t = new Thread(new Runnable() { public void run () { try { save(out); // this save method can throw IOException } catch (IOException e) { throw new RuntimeException("Failed to save entries to output stream", e); } } }); t.start(); return in; } private void save(OutputStream out) throws IOException {...} I have read How to catch an Exception from a thread but felt my question is still different, because I want to re-throw the exception in parent thread, the question above solves the problem that catches the exception only
Future,CallableandExecutorService. I'd like to know why those don't work for you before trying to re-invent them.Runnabledoesn't throw exceptions, butCallabledoes (it's the new improvedRunnable) and if you pass a Callable to an ExecutorService you get back a Future that does the work of passing any exception back to you.Runnableinterface and the classes that use it were not designed with exceptions in mind. In fact, a Runnable object'srun()method can throw unchecked exceptions and otherThrowableobjects, and that is the reason why everyThreadhas to have an uncaught exception handler.