@Override protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException { try { // first, finish any hash batches that end before the scanned row while (nextSourceKey != null && key.compareTo(nextSourceKey) >= 0) { moveToNextBatch(context); } // next, add the scanned row (as long as we've reached the first batch) if (targetHasher.isBatchStarted()) { targetHasher.hashResult(value); } } catch (Throwable t) { mapperException = t; Throwables.propagateIfInstanceOf(t, IOException.class); Throwables.propagateIfInstanceOf(t, InterruptedException.class); Throwables.propagate(t); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { if (cause instanceof IllegalArgumentException) { write(ctx, HttpResponseStatus.BAD_REQUEST, Optional.of(cause.getMessage())); } else { write(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR, Optional.of(Throwables.getStackTraceAsString(cause))); } } }
private static <T> T get(CompletableFuture<T> future) throws IOException { try { return future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw (IOException) new InterruptedIOException().initCause(e); } catch (ExecutionException e) { Throwable cause = e.getCause(); Throwables.propagateIfPossible(cause, IOException.class); throw new IOException(cause); } } }
public ResultHasher() { try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { Throwables.propagate(e); } }
/** * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link * RuntimeException}, {@link Error}, or {@code declaredType}. Example usage: * * <pre> * try { * someMethodThatCouldThrowAnything(); * } catch (IKnowWhatToDoWithThisException e) { * handle(e); * } catch (Throwable t) { * Throwables.propagateIfPossible(t, OtherException.class); * throw new RuntimeException("unexpected", t); * } * </pre> * * @param throwable the Throwable to possibly propagate * @param declaredType the single checked exception type declared by the calling method */ @GwtIncompatible // propagateIfInstanceOf public static <X extends Throwable> void propagateIfPossible( @Nullable Throwable throwable, Class<X> declaredType) throws X { propagateIfInstanceOf(throwable, declaredType); propagateIfPossible(throwable); }
/** * Returns the Method that can be used to resolve an individual StackTraceElement, or null if that * method cannot be found (it is only to be found in fairly recent JDKs). */ @GwtIncompatible // java.lang.reflect @Nullable private static Method getGetMethod() { return getJlaMethod("getStackTraceElement", Throwable.class, int.class); }
@Override public boolean filterRow() throws IOException { ipcHandlerThread = Thread.currentThread(); try { LOG.info("Handler thread " + ipcHandlerThread + " sleeping in filter..."); Thread.sleep(1000); } catch (InterruptedException e) { Throwables.propagate(e); } return super.filterRow(); } }
/** * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link * RuntimeException}, {@link Error}, {@code declaredType1}, or {@code declaredType2}. In the * unlikely case that you have three or more declared checked exception types, you can handle them * all by invoking these methods repeatedly. See usage example in {@link * #propagateIfPossible(Throwable, Class)}. * * @param throwable the Throwable to possibly propagate * @param declaredType1 any checked exception type declared by the calling method * @param declaredType2 any other checked exception type declared by the calling method */ @GwtIncompatible // propagateIfInstanceOf public static <X1 extends Throwable, X2 extends Throwable> void propagateIfPossible( @Nullable Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2) throws X1, X2 { checkNotNull(declaredType2); propagateIfInstanceOf(throwable, declaredType1); propagateIfPossible(throwable, declaredType2); }
/** * Returns the Method that can be used to return the size of a stack, or null if that method * cannot be found (it is only to be found in fairly recent JDKs). */ @GwtIncompatible // java.lang.reflect @Nullable private static Method getSizeMethod() { return getJlaMethod("getStackTraceDepth", Throwable.class); }
@Override public LocatedBlock addBlock(ClientProtocol namenode, String src, String clientName, ExtendedBlock previous, DatanodeInfo[] excludeNodes, long fileId, String[] favoredNodes) throws IOException { try { return (LocatedBlock) addBlockMethod.invoke(namenode, src, clientName, previous, excludeNodes, fileId, favoredNodes); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { Throwables.propagateIfPossible(e.getTargetException(), IOException.class); throw new RuntimeException(e); } } };
private static IOError getIOError(Throwable throwable) { IOError error = new IOErrorWithCause(throwable); error.setMessage(Throwables.getStackTraceAsString(throwable)); return error; }
@Override protected void cleanup(Context context) throws IOException, InterruptedException { if (mapperException == null) { try { finishRemainingHashRanges(context); } catch (Throwable t) { mapperException = t; } } try { sourceTable.close(); targetTable.close(); sourceConnection.close(); targetConnection.close(); } catch (Throwable t) { if (mapperException == null) { mapperException = t; } else { LOG.error("Suppressing exception from closing tables", t); } } // propagate first exception if (mapperException != null) { Throwables.propagateIfInstanceOf(mapperException, IOException.class); Throwables.propagateIfInstanceOf(mapperException, InterruptedException.class); Throwables.propagate(mapperException); } }
LOG.debug( "Source cell: " + sourceCell + " target cell: " + targetCell); Throwables.propagate(t);
@Override public LocatedBlock addBlock(ClientProtocol namenode, String src, String clientName, ExtendedBlock previous, DatanodeInfo[] excludeNodes, long fileId, String[] favoredNodes) throws IOException { try { return (LocatedBlock) addBlockMethod.invoke(namenode, src, clientName, previous, excludeNodes, fileId, favoredNodes, null); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { Throwables.propagateIfPossible(e.getTargetException(), IOException.class); throw new RuntimeException(e); } } };
@Override public void createTable(ByteBuffer in_tableName, List<ColumnDescriptor> columnFamilies) throws IOError, IllegalArgument, AlreadyExists { TableName tableName = getTableName(in_tableName); try { if (getAdmin().tableExists(tableName)) { throw new AlreadyExists("table name already in use"); } HTableDescriptor desc = new HTableDescriptor(tableName); for (ColumnDescriptor col : columnFamilies) { HColumnDescriptor colDesc = ThriftUtilities.colDescFromThrift(col); desc.addFamily(colDesc); } getAdmin().createTable(desc); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } catch (IllegalArgumentException e) { LOG.warn(e.getMessage(), e); throw new IllegalArgument(Throwables.getStackTraceAsString(e)); } }
Throwables.propagateIfInstanceOf(exceptions.firstElement(), IOException.class); throw Throwables.propagate(exceptions.firstElement());
LOG.debug("Source cell: " + sourceCell + " target cell: " + targetCell); Throwables.propagate(t);
private long write(Consumer<CompletableFuture<Long>> action) throws IOException { CompletableFuture<Long> future = new CompletableFuture<>(); action.accept(future); try { return future.get().longValue(); } catch (InterruptedException e) { InterruptedIOException ioe = new InterruptedIOException(); ioe.initCause(e); throw ioe; } catch (ExecutionException e) { Throwables.propagateIfPossible(e.getCause(), IOException.class); throw new RuntimeException(e.getCause()); } }
msg += "\nCause:\n" + Throwables.getStackTraceAsString(cause);
@Override protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException { try { // first, finish any hash batches that end before the scanned row while (nextSourceKey != null && key.compareTo(nextSourceKey) >= 0) { moveToNextBatch(context); } // next, add the scanned row (as long as we've reached the first batch) if (targetHasher.isBatchStarted()) { targetHasher.hashResult(value); } } catch (Throwable t) { mapperException = t; Throwables.propagateIfInstanceOf(t, IOException.class); Throwables.propagateIfInstanceOf(t, InterruptedException.class); Throwables.propagate(t); } }