Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement try catch exception blocks gracefully

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to elegantly implement try catch exception blocks, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

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 {} catch 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 is an idea of functional programming put forward by java8. By packaging the code as a parameter passing behavior, that is, wrapping the code logic as a parameter and passing it to the method, Java8 series of new features tutorials can follow the official account Java technology stack search to read.

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 = 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. I won't cover the best handling of exceptions here, but take a closer look at this article, "10 Best practices for exception handling in Java programming."

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 into a string, we need to use a high-level stream to wrap the low-level stream, and then do a cache to read it. 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 close the stream, 1.7 supported]

+ lambda feature to achieve [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

After reading the above, have you mastered how to implement try catch exception blocks gracefully? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report