Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem that exceptions in lambda expressions cannot be thrown by throw

Shulou Source: shulou.com Published: 2022-06-03 09:27:55 09月21日 Update

This article mainly explains "how to solve the problem that the exception in the lambda expression can not be thrown by throw". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. next, please follow the editor's train of thought to slowly deepen, to study and learn "how to solve the problem that the exception in the lambda expression can not be thrown by throw".

Catalogue

An exception within a lambda expression cannot be thrown by throw

Problem description

Cause of the problem

Solution

Cannot throw checked exception in lambda expression

An exception occurred in the lambda expression could not throw throw the problem description

Today, when using the lambda expression, there is an exception in the lambda expression, which is ready to be thrown directly, but it still reports an error:

As the blogger uses less lambda expressions, he can be said to be confused when he first sees this problem. After all, the tips on both sides can be said to be contradictory.

If you refresh the compiler or report an error, you can only honestly find the reason.

Cause of the problem

Fortunately, Java is a relatively mature language, and our predecessors have stepped on enough holes.

The reason for this phenomenon is that the lambda expression itself has no mechanism to handle exceptions, so that when an exception is detected, it can not be solved by throwing it actively.

But the specific is not very clear, if other Taoist friends encounter this problem, welcome to discuss.

Solution

Package the tested exception as a non-checked exception, namely RuntimeException

The effect is as follows:

Although the effect is still not very satisfactory, let's say it increases the proficiency of a wave of lambda expressions.

Cannot throw checked exception in lambda expression

When throwing a checked exception, our interface should have the throw keyword, but the accept method of Consumer implemented through the lambda expression does not have a keyword, so the checked exception cannot be thrown in the lambda expression and must be eaten.

Thank you for your reading, the above is "how to solve the problem of lambda expression exception can not be thrown by throw", after the study of this article, I believe you on how to solve the problem of lambda expression exception can not be thrown throw this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Expression problem cause or learning key keyword content effect solution say clear skilled active mature contradictory unexpectedly the reason Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker NVidia Linux Huawei MariaDB