In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use lambda expressions in java 8 to handle exceptions. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Brief introduction
Lambda expressions are introduced in java 8. Lambda expressions can make our code more brief and business logic clearer, but the Functional Interface used in lambda expressions does not handle exceptions very well, because these Functional Interface provided by JDK usually do not throw exceptions, which means that we have to handle exceptions manually.
Because exceptions are divided into Unchecked Exception and checked Exception, let's discuss them separately.
Dealing with Unchecked Exception
Unchecked exception is also called RuntimeException, and RuntimeException usually occurs because there is something wrong with our code. RuntimeException does not need to be captured. That is, if you have RuntimeException, you can compile without capture.
Let's look at an example:
List integers = Arrays.asList (1, 2, 3, 4, 5)
Integers.forEach (I-> System.out.println (1 / I))
This example can be compiled successfully, but there is a problem above. If there is a 0 in list, ArithmeticException will be thrown.
Although this is a Unchecked Exception, we still want to deal with it:
Integers.forEach (I-> {)
Try {
System.out.println (1 / I)
} catch (ArithmeticException e) {
System.err.println (
"Arithmetic Exception occured:" + e.getMessage ()
}
});
In the above example, we used try,catch to handle exceptions, which is simple but breaks the best practices of lambda expressions. The code becomes bloated.
Let's move try,catch into a wrapper method:
Static Consumer lambdaWrapper (Consumer consumer) {
Return I-> {
Try {
Consumer.accept (I)
} catch (ArithmeticException e) {
System.err.println (
"Arithmetic Exception occured:" + e.getMessage ()
}
}
}
The original call looks like this:
Integers.forEach (lambdaWrapper (I-> System.out.println (1 / I))
But the above wrapper fixed the capture ArithmeticException, and we adapted it into a more generic class:
Static Consumer
ConsumerWrapperWithExceptionClass (Consumer consumer, Class clazz) {
Return I-> {
Try {
Consumer.accept (I)
} catch (Exception ex) {
Try {
E exCast = clazz.cast (ex)
System.err.println (
"Exception occured:" + exCast.getMessage ()
} catch (ClassCastException ccEx) {
Throw ex
}
}
}
}
The above class passes in a class and cast it to the exception, and if it can cast, handle it, otherwise throw an exception.
After doing this, we call:
Integers.forEach (
ConsumerWrapperWithExceptionClass (
I-> System.out.println (1 / I)
ArithmeticException.class))
Dealing with checked Exception
Checked Exception is an exception that must be handled, so let's look at an example:
Static void throwIOException (Integer integer) throws IOException {
}
List integers = Arrays.asList (1,2,3,4,5)
Integers.forEach (I-> throwIOException (I)
Above we defined a method to throw IOException, which is a checked Exception that needs to be handled, so in the following forEach, the program will fail to compile because the corresponding exception is not handled.
The easiest way is to live in try,catch, as shown below:
Integers.forEach (I-> {)
Try {
ThrowIOException (I)
} catch (IOException e) {
Throw new RuntimeException (e)
}
});
Of course, the disadvantages of this approach have been discussed above, and again, we can define a new wrapper method:
Static Consumer consumerWrapper (
ThrowingConsumer throwingConsumer) {
Return I-> {
Try {
ThrowingConsumer.accept (I)
} catch (Exception ex) {
Throw new RuntimeException (ex)
}
}
}
We call it like this:
Integers.forEach (consumerWrapper (I-> throwIOException (I))
We can also encapsulate the exception:
Static Consumer consumerWrapperWithExceptionClass (
ThrowingConsumer throwingConsumer, Class exceptionClass) {
Return I-> {
Try {
ThrowingConsumer.accept (I)
} catch (Exception ex) {
Try {
E exCast = exceptionClass.cast (ex)
System.err.println (
"Exception occured:" + exCast.getMessage ()
} catch (ClassCastException ccEx) {
Throw new RuntimeException (ex)
}
}
}
}
Then call the following:
Integers.forEach (consumerWrapperWithExceptionClass (
I-> throwIOException (I), IOException.class); the above is how to use lambda expressions to handle exceptions in java 8. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.