java compiler says this exception is never thrown in body of corresponding try statement - but it _is_ thrown
By : TheLun4tic
Date : March 29 2020, 07:55 AM
wish of those help It isn't throwing an UnknownHostException. It's just appearing in the message of the exception you actually caught. It's likely the underlying root cause of the exception you caught. To determine the actual exception, you should print a bit more detail. E.g. code :
} catch (Exception e) {
logger.error("Caught Exception in login(): " + e.getClass().getName() + ": " + e.getMessage());
}
} catch (Exception e) {
logger.error("Caught Exception in login(): " + e);
}
} catch (Exception e) {
logger.error("Caught Exception in login(): " + e.getMessage(), e);
}
} catch (ClientTransportException e) {
if (e.getCause() instanceof UnknownHostException) {
// UHE.
} else {
// Other.
}
}
|
Java 8: Lambda-Streams, Filter by Method with Exception
By : Bombybuster
Date : March 29 2020, 07:55 AM
To fix this issue I have a problem trying out the Lambda expressions of Java 8. Usually it works fine, but now I have methods that throw IOException's. It's best if you look at the following code: , You must catch the exception before it escapes the lambda: code :
s = s.filter(a -> { try { return a.isActive(); }
catch (IOException e) { throw new UncheckedIOException(e); }}});
public static <T> T uncheckCall(Callable<T> callable) {
try { return callable.call(); }
catch (RuntimeException e) { throw e; }
catch (Exception e) { throw new RuntimeException(e); }
}
return s.filter(a -> uncheckCall(a::isActive))
.map(Account::getNumber)
.collect(toSet());
public static <T> T uncheckCall(Callable<T> callable) {
try { return callable.call(); }
catch (Exception e) { return sneakyThrow(e); }
}
public static void uncheckRun(RunnableExc r) {
try { r.run(); } catch (Exception e) { sneakyThrow(e); }
}
public interface RunnableExc { void run() throws Exception; }
@SuppressWarnings("unchecked")
private static <T extends Throwable> void sneakyThrow(Throwable t) throws T {
throw (T) t;
}
|
Java streams findAny() encounters null pointer exception after filter() operation filters out everything
By : I Finlay
Date : March 29 2020, 07:55 AM
Hope that helps I am having trouble figuring why findAny() throws a null pointer exception after filter() operation on a stream. In this particular test case, the filter operation should have filtered out everything, leaving no results for findAny(). , The best way to avoid NPE is: code :
Optional<JsonNode> encryption = sseEncryptionList.stream()
.filter(Objects::nonNull)
.filter(n -> "AES256".equals(n.textValue()))
.findAny();
|
Is there a way to get the item being mapped when an exception is thrown akka streams?
By : user2306327
Date : March 29 2020, 07:55 AM
it should still fix some issue Not with a Supervision.Decide but you could achieve it in a different way. Check out this program: code :
object Streams extends App{
implicit val system = ActorSystem("test")
implicit val mat = ActorMaterializer()
val source = Source(List("1", "2", "3")).map { item =>
Try {
if (item == "2") {
throw new RuntimeException("Error")
} else {
item
}
}
}
source
.alsoTo(
Flow[Try[String]]
.filter(_.isFailure)
.to(Sink.foreach(t => println("failure: " + t))))
.to(
Flow[Try[String]]
.filter(_.isSuccess)
.to(Sink.foreach(t => println("success " + t)))).run()
}
success Success(1)
failure: Failure(java.lang.RuntimeException: Error)
success Success(3)
|
How do Java 8 parallel streams behave on a thrown exception?
By : Dominykas Zigas
Date : March 29 2020, 07:55 AM
To fix the issue you can do When an exception is thrown in one of the stages, it does not wait for other operations to finish, the exception is re-thrown to the caller. That is how ForkJoinPool handles that. In contrast findFirst for example when run in parallel, will present the result to the caller only after ALL operations have finished processing (even if the result is known before the need to finish of all operations).
|