In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to avoid the use of Java exceptions as a control flow". The explanation in this article is simple and clear, and is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to avoid Java exceptions as a control flow".
Java is a general programming language, it has many different solutions to solve some special problems. However, there are both best practices that need to be followed and bad practices that are still widely used.
Using Exceptions as control flow is one of these common bad practices. There are two reasons to avoid using this method: first, it reduces the response performance of the code per unit time, and the code can be very difficult to read.
Use the following example to see how exceptions are used as control flows. The business use case for this code is:
PublicstaticintfindAge (String name) {try {String ageAsString = findUser (name); return ageAsString.length ();} catch (NameNotFoundException e) {return0;}} privatestaticStringfindUser (String name) {if (name==null) {thrownew NameNotFoundException ();} return name;}
If the user provides a non-empty name to the findAge method, it returns the length of that name. But if the user name is empty, the findUser method throws NameNotFoundException, in which case the findAge method returns 0.
So how do you ReFactor this code to avoid exceptions?
To be honest, there are many ways to do this, and only one is introduced here.
PublicstaticintfindAgeNoEx (String name) {String ageAsString = findUserNoEx (name); return ageAsString.length ();} privatestaticStringfindUserNoEx (String name) {if (name==null) {return ";} return name;}
To find out how the exception affects code performance, prepare the following code, where both implementations are executed 10 million times, and the exception took thousands of milliseconds to run on Intel Core i7-3630QM CPU.
PublicclassControlFlowWithExceptionOrNot {publicstaticclassNameNotFoundExceptionextends RuntimeException {privatestaticfinallong serialVersionUID = 3L;} privatestaticfinalint TRIAL = 1000000000; publicstaticvoid main (String [] args) throws InterruptedException {long start = System.currentTimeMillis (); for (int I = 0; I < TRIAL; iTunes +) {findAgeNoEx (null);} System.out.println ("Duration:" + (System.currentTimeMillis ()-start)); long start2 = System.currentTimeMillis (); for (int I = 0; I < TRIAL; iTunes +) {findAge (null) } System.out.println ("Duration:" + (System.currentTimeMillis ()-start2));}; publicstaticint findAge (String name) {try {String ageAsString = findUser (name); return ageAsString.length ();} catch (NameNotFoundException e) {return0;}} privatestaticString findUser (String name) {if (name = = null) {thrownew NameNotFoundException ();} return name;} publicstaticint findAgeNoEx (String name) {String ageAsString = findUserNoEx (name); return ageAsString.length () } privatestaticString findUserNoEx (String name) {if (name = = null) {return ";} return name;}}
Output:
Duration: 16 Duration: 6212
If you compare the readability of the two findAge methods, you can find that the one with no exception is very clear. No matter what the findUser method returns, the findAge method will get its length, and we are sure that the findUser method will return a string. But the one with an exception is confusing, the findUser method returns ambiguously, it may return a string or throw an exception, and this is not visible on the method's signature. Because of this, exceptions are not welcome in the functional programming paradigm.
Thank you for reading, the above is the content of "how to avoid the use of Java exceptions as control flow". After the study of this article, I believe you have a deeper understanding of how to avoid the use of Java exceptions as control flow, and the specific use 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: 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.