Use try-catch-finally in Java some noteworthy things (must SEE) _java

Source: Internet
Author: User
Tags finally block

We know that a try is responsible for outlining code that can cause exceptions, and that the catch is responsible for handling possible exceptions in a try, such as logging an error log, enabling the business to function properly, and finally being responsible for the aftermath of resource release, regardless of whether there are any exceptions that must be executed. It is generally put in the finally. What would happen if catch and finally also get an exception?

Try
    {
      //java.lang.ArithmeticException
      int a = 1/0;
    }
    catch (Exception e)
    {
      System.out.println ("catch");

      Java.lang.NullPointerException
      String value = null;
      System.out.println (Value.length ());
    }
    Finally
    {
      System.out.println ("finally");

      Java.lang.ArrayIndexOutOfBoundsException
      int[] array = {1, 2, 3};
      System.out.println (Array[6]);
    

The final result of this code is that it throws a arrayindexoutofboundsexception. The code in try has an exception, and the corresponding catch is executed;

The catch has an exception, it executes finally, and if finally there is an exception, it is thrown directly to the JVM because it is not processed. If this is not used

Finally block, then throws the NullPointerException.

This means that the exceptions that occur in catch and finally will also be thrown directly, and if we do not process them, we will generate an error at runtime. This reminds us that if catch and finally can also have exceptions, then you must use Try-catch again for processing.

The above Java use try-catch-finally some noteworthy things (must SEE) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.