In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to share with you Mybatis Plus's lambda expression query exception handling of the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you have something to gain after reading this article, let's take a look at it.
New version of lambda expression query exception
When using the new Mybatis Plus tool, the new query supports lambda expressions.
Pay attention
When using it, be sure to pay attention to whether the designed field name is standard. Field names are not allowed to start with is get. The responsible mybatis plus will make an error when compiling lambda expressions.
How to handle lambda expression exception
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 System.out.println 2, 3 4, 5); integers.forEach (I-> 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 ("ArithmeticException 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 ("ArithmeticException 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 ConsumerconsumerWrapperWithExceptionClass (Consumerconsumer, 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:
Ntegers.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:
Ntegers.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)); these are all the contents of this article entitled "handling exceptions in Mybatis Plus's lambda expression query". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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: 211
*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.