Java Exception Handling

Source: Internet
Author: User

Exception handling

1.try...catch Catching exceptions

The curly brackets of the try and catch blocks cannot be omitted

Catch a small exception when catching an exception, and then catch a large exception

Java7 start a catch block to catch multiple exceptions, multiple exceptions "|" Separate

    

Access exception information

  

2. Use finally to reclaim resources

Garbage collection in Java only reclaims resources consumed by objects in heap memory, and physical resources need to be explicitly recycled

Finally blocks are always executed (unless the SYSTEM.EXIT (1) statement is called to exit the virtual machine)

Finally, try not to use return or throw and other statements that lead to the termination of the method (because the return statement in the Try,,,catch,,,,, is not executed immediately after the call, but first to execute the FINALLY statement, executed and then back to try to execute, if, After a return is called in the Finall, the try is no longer executed)

3. Exception Handling Nesting

4. A try statement (JAVA7) that automatically closes a resource

Try the parentheses to achieve resource closure, to ensure that resources are closed, this resource must implement the Autocloseable or Closeable interface, the implementation of both interfaces, must be close ()

5.Checked anomaly and runtime anomaly system

Checked exceptions must be handled explicitly, with catch or throws

6. Throwing Exceptions using throws

7. Throw with throw: throws an exception by itself, only throws an instance

    

8. Custom exception class: Inherit exception or RuntimeException

9. Throw the exception again with throw in catch

10. Abnormal chain

Exception tracking stack for 11.Java

12. Exception Handling Rules

Java Exception Handling

Related Article

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.