Java Exception related knowledge

Source: Internet
Author: User

In some cases the programmer thinks it is an exception, and the JVM appears to be normal, such as a negative age, so in this case we need to create and throw the exception manually, which requires a throw.

Exception e = new Exception ("Age Exception");//Create exception Object

Throw e;//throws an exception

Objects created and thrown by the programmer themselves must be handled appropriately by throws or try{}, catch{}.

If the exception is thrown to the top, no catch will display the exception information on the front level. When the code in the try{} block generates an exception, the code in the try{} block is interrupted and captured by the catch{} block to the resulting exception, and the code behind the try{}, catch{} is still executed normally. If the two exceptions belong to an inheritance relationship, they should be processed in order from child to parent.

Since Java has a garbage collector, there is no need to deal with the problem of memory recycling in exception handling, but there are still some resources that need to be handled by programmers, such as files, network connections, and pictures, which can be try{}, catch{} and finally{} blocks to clean up the program. Regardless of whether an exception occurs in the try{} block, the code in the finally{} block is bound to be executed.

Usually in a method (class) declaration through the throws declaration method (class) may throw exception information, and in the method (class) through the throw declaration of a specific exception information; Throw can only be used to throw an exception, and throws may throw multiple exceptions, such as: throws exception1,exception2{}.

Java Exception related knowledge

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.