“This is the 18th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021.”

The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!

May you keep your love and go to the mountains and seas in the coming days!

Note: yesterday we had a little summary of exceptions, so let’s take a look at what new JDK1.7 features bring to exceptions. Walking up and down? Let’s have a look!

😛JDK1.7 about new exception features

📤 try – with – resources

There are many resources in the Java class library that need to be closed by the close method. Such as InputStream, OutputStream, etc. As a developer, you often ignore the resource shutdown method, resulting in memory leaks. Of course not me!

Prior to JDK1.7, try-catch-finally statements were the best way to ensure that a resource would be closed, even if it was an exception or a return.

  • Let’s take a look at how we closed the resource earlier:

    public static String readFile(String path) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(path)); return br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally {// The resource must be closed here if (br! = null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }}Copy the code

    Do we have to manually close the resource in the finally statement block, otherwise it will leak

  • In JDK1.7 and later:

    The try-with-resources statement was introduced in JDK1.7.

    • try-with-resourcesA statement is a try statement that declares one or more resources.try-with-resourcesThe statement ensures that at the end of the statement every resource is closed, as long as objects implementing either the AutoCloseable or Closeable interfaces are availabletry-with-resourcesTo implement exception handling and close resources.
    • In fact, this is also done at compile timetry-catch-finallyStatements.

    Let’s see how it works:

    Format:

    Try (create stream object statements, if multiple, use '; ') {// Read and write data} catch (IOException e) {e.printStackTrace(); }Copy the code

    Presentation:

    /** * public class TryWithResourcesDemo {public static. /** * public class TryWithResourcesDemo {public static String readLineFormFile(String path) { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } catch (IOException e) { e.printStackTrace(); } return null; }}Copy the code
  • A comparison of the two:

    • Code refinement. Before JDK1.7, there were finally blocks. If you use some frameworks, you may hand the finally block to the framework, for exampleSpring. JDK1.7 and later will automatically execute a try block if the resource class implements an AutoCloseable or Closeable programclose()Whatever resources are usedbr.readLine()Whether an exception is thrown.
    • The code is more complete. In many cases where a resource leak occurs, the developer does not or does not shut down the resource properly. Adopted after JDK1.7try-with-resourcesYou can hand over to the JVM the work of shutting down resources, which is not directly related to the business implementation. Part of the code risk that may occur during development is eliminated.
    • In order toreadLineFormFileMethod for example, if calledreadLine()andclose()Methods all throw exceptions, and the latter exception is disallowed to preserve the first.

🌸 summary

We are going to learn some new features of JDK1.7. We are going to learn some new features of JDK1.7. Look forward to more in the next chapter! Welcome to the next chapter!

Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little, please kindly and lovely talent leaders to give a thumb-up, favorites, one key three even, thank you very much!

Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!

Thank you all for seeing this! May you live up to your youth and have no regrets!

\