Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 234

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report