In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to handle exceptions in Java8". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
As we all know, Java exceptions are divided into checked exceptions and non-checked exceptions. Checking exceptions is the exception that the compiler requires developers to handle, while non-checking exceptions do not. So when we need to call a method that throws a check exception, we must explicitly capture it:
MyList.stream () .map (item-> try {return doSomething (item);} catch (MyException e) {throw new RuntimeException (e);}}) .forEach (System.out::printion)
As shown in the code above, we caught the MyException check exception, then converted it to a RuntimeException non-check exception and threw it again. But you know very well in your heart that this is not the best way to deal with it.
Optimization 1: improve readability
As shown below, we extract the method body into the trySomething method separately, so that we can complete the lambda expression in one line of code, and the readability of the whole code will be greatly improved:
MyList.stream () .map (this::trySomething) .forEach (System.out::printion); private Item trySomething (Item item) {try {return doSomething (item);} catch (MyException e) {throw new RuntimeException (e);}} Optimization II: reuse code
Now that you have solved the above problem, when we come across other ways to handle exceptions, do we all have to use try. Catch... Would you like to wrap it on one layer? In that case, you can imagine that this kind of writing may be everywhere in the code. To avoid getting caught up in this repetitive writing, we should abstract the above code snippet into a small utility class dedicated to doing this. You only need to define it once, and then call it many times where you need it.
To achieve this, we first need to define a functional interface ourselves, which may throw an exception:
Then, let's write a static helper function wrap, which takes a functional interface parameter, catches a check exception in the method body, and throws a non-check exception RuntimeException:
With the wrap static function, you can now write this in a lambda expression
Optimization 3: continue to run when an exception occurs
The readability and abstraction of the above code is good, but there is a big problem, that is, when an exception occurs, your stream code will stop immediately and will not move on to the next element. In most cases, we may want to keep stream running when an exception is thrown.
Instead of throwing an exception and treating the exception as a special case, we might as well directly treat the exception as a "normal" return value. That is, this function returns either a correct result or an exception, so we now need to define a new wrapper class Either to store both results. For convenience, we store the exception in the left field and the normally returned value in the right field. Here is a simple example of the Either class:
Public class Eithercl {private final L Left: private final R right; private Either (L left, R right) {this left=left; this right = right;} public static Either, Left (L value) {return new Either (value, null):} public static Either Right (R value) {return new Either (null, value)} public Optional getleft () {return Optional. Ofnullable (left)} public Optional getright () {return Optional.ofnullable (right);} public boolean isleft () {return left I-null;} public boolean isright () {return right! = null;} public
< T>Optional mapleft (Function
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.