io.pravega.common.Exceptions.sneakyThrow()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(113)

本文整理了Java中io.pravega.common.Exceptions.sneakyThrow()方法的一些代码示例,展示了Exceptions.sneakyThrow()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Exceptions.sneakyThrow()方法的具体详情如下:
包路径:io.pravega.common.Exceptions
类名称:Exceptions
方法名:sneakyThrow

Exceptions.sneakyThrow介绍

[英]Throws any throwable 'sneakily' - you don't need to catch it, nor declare that you throw it onwards. The exception is still thrown - javac will just stop whining about it.

Example usage:

public void run() { 
throw sneakyThrow(new IOException("You don't need to catch me!")); 
}

NB: The exception is not wrapped, ignored, swallowed, or redefined. The JVM actually does not know or care about the concept of a 'checked exception'. All this method does is hide the act of throwing a checked exception from the java compiler.

Note that this method has a return type of RuntimeException; it is advised you always call this method as argument to the throw statement to avoid compiler errors regarding no return statement and similar problems. This method won't of course return an actual RuntimeException - it never returns, it always throws the provided exception.
[中]

代码示例

代码示例来源:origin: pravega/pravega

private RuntimeException handelUnexpectedReply(Reply reply) {
  closeConnection(reply.toString());
  if (reply instanceof WireCommands.NoSuchSegment) {
    throw new NoSuchSegmentException(reply.toString());
  } else if (reply instanceof SegmentIsSealed) {
    throw Exceptions.sneakyThrow(new SegmentSealedException(reply.toString()));
  } else if (reply instanceof WrongHost) {
    throw Exceptions.sneakyThrow(new ConnectionFailedException(reply.toString()));
  } else {
    throw Exceptions.sneakyThrow(new ConnectionFailedException("Unexpected reply of " + reply + " when expecting an AppendSetup"));
  }
}

代码示例来源:origin: pravega/pravega

} catch (InterruptedException e) {
  Thread.currentThread().interrupt();
  throw Exceptions.sneakyThrow(e);
} catch (Exception e) {
  throw Exceptions.sneakyThrow(Exceptions.unwrap(e));

代码示例来源:origin: pravega/pravega

@Override
  public void close() {
    watch.getAndUpdate(x -> {
      if (x != null) {
        try {
          x.close();
        } catch (IOException e) {
          throw Exceptions.sneakyThrow(e);
        }
      }
      return x;
    });

    gcExecutor.shutdown();
  }
}

代码示例来源:origin: pravega/pravega

@Override
public CompletableFuture<Boolean> isStreamCutValid(Map<Long, Long> streamCut) {
  return Futures.allOfWithResults(streamCut.keySet().stream().map(x -> getSegment(x).thenApply(segment ->
      new SimpleEntry<>(segment.getKeyStart(), segment.getKeyEnd())))
                       .collect(Collectors.toList()))
         .thenAccept(x -> RecordHelper.validateStreamCut(new ArrayList<>(x)))
         .handle((r, e) -> {
           if (e != null) {
             if (Exceptions.unwrap(e) instanceof IllegalArgumentException) {
               return false;
             } else {
               log.warn("Exception while trying to validate a stream cut for stream {}/{}", scope, name);
               throw Exceptions.sneakyThrow(e);
             }
           } else {
             return true;
           }
         });
}

代码示例来源:origin: pravega/pravega

private <T> T throwException(String segmentName, Exception e) throws StreamSegmentException {
  if (e instanceof NoSuchFileException || e instanceof FileNotFoundException) {
    throw new StreamSegmentNotExistsException(segmentName);
  }
  if (e instanceof FileAlreadyExistsException) {
    throw new StreamSegmentExistsException(segmentName);
  }
  if (e instanceof IndexOutOfBoundsException) {
    throw new IllegalArgumentException(e.getMessage());
  }
  if (e instanceof AccessControlException
      || e instanceof AccessDeniedException
      || e instanceof NonWritableChannelException) {
    throw new StreamSegmentSealedException(segmentName, e);
  }
  throw Exceptions.sneakyThrow(e);
}

代码示例来源:origin: pravega/pravega

private <T> T throwException(String segmentName, Exception e) throws StreamSegmentException {
  if (e instanceof S3Exception) {
    S3Exception s3Exception = (S3Exception) e;
    String errorCode = Strings.nullToEmpty(s3Exception.getErrorCode());
    if (errorCode.equals("NoSuchKey")) {
      throw new StreamSegmentNotExistsException(segmentName);
    }
    if (errorCode.equals("PreconditionFailed")) {
      throw new StreamSegmentExistsException(segmentName);
    }
    if (errorCode.equals("InvalidRange")
        || errorCode.equals("InvalidArgument")
        || errorCode.equals("MethodNotAllowed")
        || s3Exception.getHttpCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
      throw new IllegalArgumentException(segmentName, e);
    }
    if (errorCode.equals("AccessDenied")) {
      throw new StreamSegmentSealedException(segmentName, e);
    }
  }
  if (e instanceof IndexOutOfBoundsException) {
    throw new ArrayIndexOutOfBoundsException(e.getMessage());
  }
  throw Exceptions.sneakyThrow(e);
}

代码示例来源:origin: pravega/pravega

@Override
public void deleteReaderGroup(String groupName) {
  getAndHandleExceptions(controller.sealStream(scope, getStreamForReaderGroup(groupName))
                   .thenCompose(b -> controller.deleteStream(scope,
                                        getStreamForReaderGroup(groupName)))
                   .exceptionally(e -> {
                     if (e instanceof InvalidStreamException) {
                       return null;
                     } else {
                       log.warn("Failed to delete stream", e);
                     }
                     throw Exceptions.sneakyThrow(e);
                   }),
              RuntimeException::new);
  
}

代码示例来源:origin: pravega/pravega

public CompletableFuture<List<NodeUri>> getControllerServerList() {
  if (cluster == null) {
    return Futures.failedFuture(new IllegalStateException("Controller cluster not initialized"));
  }
  return CompletableFuture.supplyAsync(() -> {
    try {
      return cluster.getClusterMembers().stream()
          .map(host -> NodeUri.newBuilder().setEndpoint(host.getIpAddr()).setPort(host.getPort()).build())
          .collect(Collectors.toList());
    } catch (ClusterException e) {
      // cluster implementation throws checked exceptions which cannot be thrown inside completable futures.
      throw Exceptions.sneakyThrow(e);
    }
  }, executor);
}

代码示例来源:origin: pravega/pravega

throw Exceptions.sneakyThrow(e1);
      return null;
    throw Exceptions.sneakyThrow(t);
  });
}, connectionFactory.getInternalExecutor());

代码示例来源:origin: pravega/pravega

/**
 * Translates HDFS specific Exceptions to Pravega-equivalent Exceptions.
 *
 * @param segmentName Name of the stream segment on which the exception occurs.
 * @param e           The exception to be translated.
 * @return  The exception to be thrown.
 */
static <T> StreamSegmentException convertException(String segmentName, Throwable e) {
  if (e instanceof RemoteException) {
    e = ((RemoteException) e).unwrapRemoteException();
  }
  if (e instanceof PathNotFoundException || e instanceof FileNotFoundException) {
    return new StreamSegmentNotExistsException(segmentName, e);
  } else if (e instanceof FileAlreadyExistsException || e instanceof AlreadyBeingCreatedException) {
    return new StreamSegmentExistsException(segmentName, e);
  } else if (e instanceof AclException) {
    return new StreamSegmentSealedException(segmentName, e);
  } else {
    throw Exceptions.sneakyThrow(e);
  }
}

代码示例来源:origin: pravega/pravega

@Override
public void addReader(String process, String readerGroup, String readerId) throws CheckpointStoreException {
  String path = getReaderGroupPath(process, readerGroup);
  try {
    updateReaderGroupData(path, groupData -> {
      if (groupData.getState() == ReaderGroupData.State.Sealed) {
        throw Exceptions.sneakyThrow(new CheckpointStoreException(CheckpointStoreException.Type.Sealed,
            "ReaderGroup is sealed"));
      }
      List<String> list = groupData.getReaderIds();
      if (list.contains(readerId)) {
        throw Exceptions.sneakyThrow(new CheckpointStoreException(CheckpointStoreException.Type.NodeExists,
            "Duplicate readerId"));
      }
      list.add(readerId);
      return new ReaderGroupData(groupData.getState(), list);
    });
    addNode(getReaderPath(process, readerGroup, readerId));
  } catch (KeeperException.NoNodeException e) {
    throw new CheckpointStoreException(CheckpointStoreException.Type.NoNode, e);
  } catch (KeeperException.ConnectionLossException | KeeperException.OperationTimeoutException
      | KeeperException.SessionExpiredException e) {
    throw new CheckpointStoreException(CheckpointStoreException.Type.Connectivity, e);
  } catch (CheckpointStoreException e) {
    throw e;
  } catch (Exception e) {
    throw new CheckpointStoreException(e);
  }
}

代码示例来源:origin: pravega/pravega

throw Exceptions.sneakyThrow(ex);

相关文章