Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Provides Throws.java tool classes and examples.
Source Link
木原金
  • 377
  • 2
  • 6

Using generics magic:

public class TempTest {
 @FunctionalInterface
 public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
 @Override
 @SuppressWarnings({"unchecked", "RedundantCast"})
 default void accept(T t) {
 ((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
 }
 void acceptEX(T t) throws EX;
 static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
 return consumer;
 }
 }
 void deleteEntity(Node node) {
 node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
 //or 
 node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
 }
 void deleteChild(Object child) throws SomeException {
 //some code
 throw new SomeException();
 }
 // mock class
 private static class Node {
 public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
 }
 private static class ChildNode {}
 private static class SomeException extends Throwable {}
}

You can use the Throws.java utility class, which encapsulates common Function interfaces. It helps you wrap lambdas that throw checked exceptions. For example:

Consumer<String> consumer = Throws.wrapConsumer(s -> {throw new Throwable();});

Using generics magic:

public class TempTest {
 @FunctionalInterface
 public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
 @Override
 @SuppressWarnings({"unchecked", "RedundantCast"})
 default void accept(T t) {
 ((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
 }
 void acceptEX(T t) throws EX;
 static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
 return consumer;
 }
 }
 void deleteEntity(Node node) {
 node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
 //or 
 node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
 }
 void deleteChild(Object child) throws SomeException {
 //some code
 throw new SomeException();
 }
 // mock class
 private static class Node {
 public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
 }
 private static class ChildNode {}
 private static class SomeException extends Throwable {}
}

Using generics magic:

public class TempTest {
 @FunctionalInterface
 public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
 @Override
 @SuppressWarnings({"unchecked", "RedundantCast"})
 default void accept(T t) {
 ((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
 }
 void acceptEX(T t) throws EX;
 static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
 return consumer;
 }
 }
 void deleteEntity(Node node) {
 node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
 //or 
 node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
 }
 void deleteChild(Object child) throws SomeException {
 //some code
 throw new SomeException();
 }
 // mock class
 private static class Node {
 public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
 }
 private static class ChildNode {}
 private static class SomeException extends Throwable {}
}

You can use the Throws.java utility class, which encapsulates common Function interfaces. It helps you wrap lambdas that throw checked exceptions. For example:

Consumer<String> consumer = Throws.wrapConsumer(s -> {throw new Throwable();});
corrected spelling
Source Link
木原金
  • 377
  • 2
  • 6

Use genericUsing generics magic:

public class TempTest {
 @FunctionalInterface
 public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
 @Override
 @SuppressWarnings({"unchecked", "RedundantCast"})
 default void accept(T t) {
 ((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
 }
 void acceptEX(T t) throws EX;
 static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
 return consumer;
 }
 }
 void deleteEntity(Node node) {
 node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
 //or 
 node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
 }
 void deleteChild(Object child) throws SomeException {
 //some code
 throw new SomeException();
 }
 // mock class
 private static class Node {
 public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
 }
 private static class ChildNode {}
 private static class SomeException extends Throwable {}
}

Use generic magic:

public class TempTest {
 @FunctionalInterface
 public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
 @Override
 @SuppressWarnings({"unchecked", "RedundantCast"})
 default void accept(T t) {
 ((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
 }
 void acceptEX(T t) throws EX;
 static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
 return consumer;
 }
 }
 void deleteEntity(Node node) {
 node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
 //or 
 node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
 }
 void deleteChild(Object child) throws SomeException {
 //some code
 throw new SomeException();
 }
 // mock class
 private static class Node {
 public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
 }
 private static class ChildNode {}
 private static class SomeException extends Throwable {}
}

Using generics magic:

public class TempTest {
 @FunctionalInterface
 public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
 @Override
 @SuppressWarnings({"unchecked", "RedundantCast"})
 default void accept(T t) {
 ((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
 }
 void acceptEX(T t) throws EX;
 static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
 return consumer;
 }
 }
 void deleteEntity(Node node) {
 node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
 //or 
 node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
 }
 void deleteChild(Object child) throws SomeException {
 //some code
 throw new SomeException();
 }
 // mock class
 private static class Node {
 public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
 }
 private static class ChildNode {}
 private static class SomeException extends Throwable {}
}
Post Undeleted by 木原金
Sorry, there were some mistakes in the previous code.
Source Link
木原金
  • 377
  • 2
  • 6

Use generic magic:

public class TempTest {
 @FunctionalInterface
 public interface ConsumerEX<T>ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
 @Override
 @SuppressWarnings({"unchecked", "RedundantCast"})
 default void accept(T t) {
 acceptEx((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
 }
 <EX extends Throwable> void acceptExacceptEX(T t) throws EX;
 static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
  return consumer;
 }
 }

 void deleteEntity(Node node) {
 node.getChildren().forEach((ConsumerEx<ChildNode>ConsumerEX<ChildNode, ?>) this::deleteChild);
 //or 
  node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
 }
 void deleteChild(Object child) throws SomeException {
 //some code
 throw new SomeException();
 }
 // mock class
 private static class Node {
 public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
 }
 private static class ChildNode {}
 private static class SomeException extends Throwable {}
}

Use generic magic:

public class TempTest {
 @FunctionalInterface
 public interface ConsumerEX<T> extends Consumer<T> {
 @Override
 default void accept(T t) {
 acceptEx(t);
 }
 <EX extends Throwable> void acceptEx(T t) throws EX;
 }
 void deleteEntity(Node node) {
 node.getChildren().forEach((ConsumerEx<ChildNode>) this::deleteChild);
 }
 void deleteChild(Object child) throws SomeException {
 //some code
 throw new SomeException();
 }
 // mock class
 private static class Node {
 public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
 }
 private static class ChildNode {}
 private static class SomeException extends Throwable {}
}

Use generic magic:

public class TempTest {
 @FunctionalInterface
 public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
 @Override
 @SuppressWarnings({"unchecked", "RedundantCast"})
 default void accept(T t) {
 ((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
 }
 void acceptEX(T t) throws EX;
 static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
  return consumer;
 }
 }

 void deleteEntity(Node node) {
 node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
 //or 
  node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
 }
 void deleteChild(Object child) throws SomeException {
 //some code
 throw new SomeException();
 }
 // mock class
 private static class Node {
 public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
 }
 private static class ChildNode {}
 private static class SomeException extends Throwable {}
}
Post Deleted by 木原金
Source Link
木原金
  • 377
  • 2
  • 6
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /