@Test public void testTaggedIOException() { final Serializable tag = UUID.randomUUID(); final IOException exception = new IOException("Test exception"); final TaggedIOException tagged = new TaggedIOException(exception, tag); assertTrue(TaggedIOException.isTaggedWith(tagged, tag)); assertFalse(TaggedIOException.isTaggedWith(tagged, UUID.randomUUID())); assertEquals(exception, tagged.getCause()); assertEquals(exception.getMessage(), tagged.getMessage()); }
/** * Re-throws the original exception thrown by this stream. This method * first checks whether the given exception is a {@link TaggedIOException} * wrapper created by this decorator, and then unwraps and throws the * original wrapped exception. Returns normally if the exception was * not thrown by this stream. * * @param exception an exception * @throws IOException original exception, if any, thrown by this stream */ public void throwIfCauseOf(final Exception exception) throws IOException { TaggedIOException.throwCauseIfTaggedWith(exception, tag); }
/** * Throws the original {@link IOException} if the given throwable is * a {@link TaggedIOException} decorator the given tag. Does nothing * if the given throwable is of a different type or if it is tagged * with some other tag. * <p> * This method is typically used in a <code>catch</code> block to * selectively rethrow tagged exceptions. * <pre> * Serializable tag = ...; * try { * ...; * } catch (Throwable t) { * TaggedIOExcepton.throwCauseIfTagged(t, tag); * // handle other kinds of exceptions * } * </pre> * * @param throwable an exception * @param tag tag object * @throws IOException original exception from the tagged decorator, if any */ public static void throwCauseIfTaggedWith(final Throwable throwable, final Object tag) throws IOException { if (isTaggedWith(throwable, tag)) { throw ((TaggedIOException) throwable).getCause(); } }
/** * Tags any IOExceptions thrown, wrapping and re-throwing. * * @param e The IOException thrown * @throws IOException if an I/O error occurs */ @Override protected void handleIOException(final IOException e) throws IOException { throw new TaggedIOException(e, tag); }
/** * Tests if the given exception was caused by this stream. * * @param exception an exception * @return {@code true} if the exception was thrown by this stream, * {@code false} otherwise */ public boolean isCauseOf(final Throwable exception) { return TaggedIOException.isTaggedWith(exception, tag); }
exception = ((TaggedIOException) exception).getCause();
/** * Throws the original {@link IOException} if the given throwable is * a {@link TaggedIOException} decorator the given tag. Does nothing * if the given throwable is of a different type or if it is tagged * with some other tag. * <p> * This method is typically used in a <code>catch</code> block to * selectively rethrow tagged exceptions. * <pre> * Serializable tag = ...; * try { * ...; * } catch (Throwable t) { * TaggedIOExcepton.throwCauseIfTagged(t, tag); * // handle other kinds of exceptions * } * </pre> * * @param throwable an exception * @param tag tag object * @throws IOException original exception from the tagged decorator, if any */ public static void throwCauseIfTaggedWith(final Throwable throwable, final Object tag) throws IOException { if (isTaggedWith(throwable, tag)) { throw ((TaggedIOException) throwable).getCause(); } }
/** * Tags any IOExceptions thrown, wrapping and re-throwing. * * @param e The IOException thrown * @throws IOException if an I/O error occurs */ @Override protected void handleIOException(final IOException e) throws IOException { throw new TaggedIOException(e, tag); }
/** * Tests if the given exception was caused by this stream. * * @param exception an exception * @return {@code true} if the exception was thrown by this stream, * {@code false} otherwise */ public boolean isCauseOf(final Exception exception) { return TaggedIOException.isTaggedWith(exception, tag); }
/** * Throws the original {@link IOException} if the given throwable is * a {@link TaggedIOException} decorator the given tag. Does nothing * if the given throwable is of a different type or if it is tagged * with some other tag. * <p> * This method is typically used in a <code>catch</code> block to * selectively rethrow tagged exceptions. * <pre> * Serializable tag = ...; * try { * ...; * } catch (Throwable t) { * TaggedIOExcepton.throwCauseIfTagged(t, tag); * // handle other kinds of exceptions * } * </pre> * * @param throwable an exception * @param tag tag object * @throws IOException original exception from the tagged decorator, if any */ public static void throwCauseIfTaggedWith(Throwable throwable, Object tag) throws IOException { if (isTaggedWith(throwable, tag)) { throw ((TaggedIOException) throwable).getCause(); } }
@Test public void testOtherException() throws Exception { final IOException exception = new IOException("test exception"); final InputStream closed = new ClosedInputStream(); final TaggedInputStream stream = new TaggedInputStream(closed); assertFalse(stream.isCauseOf(exception)); assertFalse(stream.isCauseOf( new TaggedIOException(exception, UUID.randomUUID()))); stream.throwIfCauseOf(exception); stream.throwIfCauseOf( new TaggedIOException(exception, UUID.randomUUID())); stream.close(); }
/** * Tests if the given exception was caused by this stream. * * @param exception an exception * @return {@code true} if the exception was thrown by this stream, * {@code false} otherwise */ public boolean isCauseOf(final Throwable exception) { return TaggedIOException.isTaggedWith(exception, tag); }
/** * Re-throws the original exception thrown by this stream. This method * first checks whether the given exception is a {@link TaggedIOException} * wrapper created by this decorator, and then unwraps and throws the * original wrapped exception. Returns normally if the exception was * not thrown by this stream. * * @param throwable an exception * @throws IOException original exception, if any, thrown by this stream */ public void throwIfCauseOf(final Throwable throwable) throws IOException { TaggedIOException.throwCauseIfTaggedWith(throwable, tag); }
/** * Throws the original {@link IOException} if the given throwable is * a {@link TaggedIOException} decorator the given tag. Does nothing * if the given throwable is of a different type or if it is tagged * with some other tag. * <p> * This method is typically used in a <code>catch</code> block to * selectively rethrow tagged exceptions. * <pre> * Serializable tag = ...; * try { * ...; * } catch (Throwable t) { * TaggedIOExcepton.throwCauseIfTagged(t, tag); * // handle other kinds of exceptions * } * </pre> * * @param throwable an exception * @param tag tag object * @throws IOException original exception from the tagged decorator, if any */ public static void throwCauseIfTaggedWith(final Throwable throwable, final Object tag) throws IOException { if (isTaggedWith(throwable, tag)) { throw ((TaggedIOException) throwable).getCause(); } }
@Test public void testOtherException() throws Exception { final IOException exception = new IOException("test exception"); final OutputStream closed = new ClosedOutputStream(); final TaggedOutputStream stream = new TaggedOutputStream(closed); assertFalse(stream.isCauseOf(exception)); assertFalse(stream.isCauseOf( new TaggedIOException(exception, UUID.randomUUID()))); try { stream.throwIfCauseOf(exception); } catch (final IOException e) { fail("Unexpected exception thrown"); } try { stream.throwIfCauseOf( new TaggedIOException(exception, UUID.randomUUID())); } catch (final IOException e) { fail("Unexpected exception thrown"); } stream.close(); }
/** * Tests if the given exception was caused by this stream. * * @param exception an exception * @return {@code true} if the exception was thrown by this stream, * {@code false} otherwise */ public boolean isCauseOf(final Throwable exception) { return TaggedIOException.isTaggedWith(exception, tag); }
/** * Re-throws the original exception thrown by this stream. This method * first checks whether the given exception is a {@link TaggedIOException} * wrapper created by this decorator, and then unwraps and throws the * original wrapped exception. Returns normally if the exception was * not thrown by this stream. * * @param exception an exception * @throws IOException original exception, if any, thrown by this stream */ public void throwIfCauseOf(Exception exception) throws IOException { TaggedIOException.throwCauseIfTaggedWith(exception, tag); }
/** * Throws the original {@link IOException} if the given throwable is * a {@link TaggedIOException} decorator the given tag. Does nothing * if the given throwable is of a different type or if it is tagged * with some other tag. * <p> * This method is typically used in a <code>catch</code> block to * selectively rethrow tagged exceptions. * <pre> * Serializable tag = ...; * try { * ...; * } catch (Throwable t) { * TaggedIOExcepton.throwCauseIfTagged(t, tag); * // handle other kinds of exceptions * } * </pre> * * @param throwable an exception * @param tag tag object * @throws IOException original exception from the tagged decorator, if any */ public static void throwCauseIfTaggedWith(Throwable throwable, Object tag) throws IOException { if (isTaggedWith(throwable, tag)) { throw ((TaggedIOException) throwable).getCause(); } }
/** * Tags any IOExceptions thrown, wrapping and re-throwing. * * @param e The IOException thrown * @throws IOException if an I/O error occurs */ @Override protected void handleIOException(IOException e) throws IOException { throw new TaggedIOException(e, tag); }
/** * Tests if the given exception was caused by this stream. * * @param exception an exception * @return {@code true} if the exception was thrown by this stream, * {@code false} otherwise */ public boolean isCauseOf(final Exception exception) { return TaggedIOException.isTaggedWith(exception, tag); }