How to implement try catch exception block elegantly?

Source: Xiao Ming's study notes blog

Address: www.cnblogs.com/liruilong/p/13403963.html

In the project, we will encounter exception handling. For runtime exceptions, we need to judge and handle ourselves. For the checked exception, we need to take the initiative to deal with it.

But the cumbersome try{}catch is nested in the code, and it looks very uncomfortable. Here we will not discuss performance, as far as the code is concerned, let's see how to hide it. The principle is the same. It becomes how to write. Let's see how to handle exception blocks gracefully.

before this. You need to know the following concepts:

  • Behavior parameterization:

  • It is an idea of ​​functional programming proposed 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, the new Java 8 series of new feature tutorials can be read by paying attention to the public account Java technology stack. .

  • Lambda expression:

  • Java8 proposes: Lambda expressions are understood as a way to concisely express passable anonymous functions. It has no name, but it has a function body, a parameter list, and a return type. An exception type can be thrown. Packaging code logic is the use of Lambda expressions for parameters .

  • Functional interface:

  • Essentially, it is an ordinary interface with only one abstract method, which can be implicitly converted into a Lambda expression, and it needs to be defined with annotations (@FunctionalInterface). Default methods and static methods do not need to be abstract methods, and can be defined in functional interfaces.

If multiple abstract methods are additionally defined in the functional interface, the signatures of these abstract methods must be the same as the public methods of Object. The interface will eventually have a certain class implementation, and the ultimate parent class of the class is Object. Therefore, functional interfaces can define public methods of Object.

This code is no stranger to my friends. This is a checked exception and a ClassNotFoundException needs to be thrown.

Normal writing:

Well, let’s look at the specific implementation: it’s very simple. What we have to do is to treat `Class<?> clazz = Class.forName("class name");` as a behavior, accept a String and get a Class, so we have to define a functional interface to describe this behavior.

Here, because our behavior needs to throw an exception. So an exception is also thrown in the interface. The best handling of exceptions is not introduced here. For details, read this " 10 Best Practices on Exception Handling in Java Programming ".

Then, we need to define a method to pass our behavior as a parameter, and at the same time, catch our exception.

Then, we can call our method classFind method,

of course. In fact, this kind of thinking is not simple to catch 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 read it out by caching. Here, we will inevitably encounter operations such as exception handling and stream closing. Below we will make these codes abnormal. Just concentrate on writing and reading logic.

My thoughts:

I am not very familiar with java IO, please leave a message if you have a good way to 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 buffer stream is to convert byte stream to character stream and then wrap it with advanced stream.

So my idea is to avoid too many IO stream closures and exception captures in the logic, and concentrate on processing the read logic, combining the following two technologies:

To perform an action, any BufferReader -> String Lambda expression can be passed in as a parameter. As long as it conforms to the signature of the peocess method.

carried out

Come on, I am willing to be treated kindly by this world^_^

Recommend to read more on my blog:

1. A series of tutorials on Java JVM, collections, multithreading, and new features

2. Spring MVC, Spring Boot, Spring Cloud series of tutorials

3. Maven, Git, Eclipse, Intellij IDEA series tool tutorial

4. The latest interview questions from major manufacturers such as Java, back-end, architecture, and Alibaba

Feel good, don’t forget to like + forward!

Finally, pay attention to the WeChat official account of the stack leader: Java technology stack, reply: Welfare, you can get a free copy of the latest Java interview questions I have compiled for 2020. It is really complete (including answers), without any routine.

Guess you like

Origin blog.csdn.net/youanyyou/article/details/108341233