In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to implement try/catch exception block elegantly? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
In the project, we will encounter exception handling, for run-time exceptions, we need to judge and handle them. We need to take the initiative to deal with the detected abnormalities.
But the tedious try {} caht is embedded in the code and looks uncomfortable. Let's not talk about performance here. As far as the code is concerned, let's see how to hide it. The principle remains the same. Become a way of writing. Let's take a look at how to handle exception blocks gracefully.
Before that. You need to know the following concepts:
Behavior parameterization:
It's an idea of functional programming put forward by java8 by wrapping the code as a parameter passing behavior, that is, wrapping the code logic as a parameter and passing it to the method.
Lambda expression:
Java8 proposed that an Lambda expression is understood as a concise way to represent an anonymous function that can be passed. It has no name, but it has a function body, a parameter list, and a return type. An exception type can be thrown. The wrapper code logic is used even with Lambda expressions for parameters.
Functional interface:
It is essentially a normal interface with only one abstract method, which can be implicitly converted to Lambda expressions, which need to be defined with annotations (@ FunctionalInterface). Default and static methods may not belong to abstract methods and can be defined in functional interfaces.
If multiple abstract methods are defined in a functional interface, then these abstract method signatures must be the same as Object's public method, and the interface ultimately has a definite class implementation, and the final parent class of the class is Object. Therefore, the functional interface can define the public method of Object.
This code is no stranger to friends. This is a checked exception and a ClassNotFoundException needs to be thrown.
Normal way of writing:
Well, let's look at the specific implementation: quite simply, what we need to do is to treat Class clazz = Class.forName ("class name") as an behavior, accept a String and get a Class, so we need to define a function interface to describe this behavior.
Here, because our behavior needs to throw an exception. So an exception is also thrown in the interface.
Then, we need to define a method that passes our behavior as a parameter, and at the same time, catch our exception.
Then we can call our method classFind method
That's for sure. In fact, this idea is not simple to do the handling of catching exceptions.
Let's look at a Demo- > text file converted to a string:
In my opinion, to convert a text file to a string, we need to wrap the low-level stream with an advanced stream and then do a cache to read it out. Here, we will inevitably encounter exception handling, flow closure and other operations, let's make these code exceptions. Just concentrate on the logic of writing and reading.
My train of thought:
I am not very familiar with java IO. If you have any good ways, please leave a message and learn from each other:
FileInputStream fileInputStream = new FileInputStream (file))
InputStreamReader inputStreamReader = new InputStreamReader (fileInputStream))
BufferedReader bufferedReader = new BufferedReader (inputStreamReader))
String str = bufferedReader.readLine ()
Byte stream-"character stream -" character cache stream is wrapped with an advanced stream after converting a byte stream into a character stream.
So my idea is to avoid too many IO stream closures and exception traps in the logic, and just concentrate on the read logic, combining the following two techniques:
Try () {} [automatically closes the stream, 1.7 supports] lambda feature to implement [behavior parameterization, 1.8]
Any Lambda expression of BufferReader-> String can be passed in as an argument. As long as the signature conforms to the peocess method.
Execution
This is the answer to the question on how to elegantly implement try/catch exception blocks. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.