Try-catch in java

Source: Internet
Author: User

The Code is as follows [java] public class TestException {public static void main (String [] args) {try {// throw new Exception ("Nothing "); throw new Error ("nothing"); // switch the two rows to try ??} Catch (Exception e) {System. out. println ("caught Exception .. "); E. printStackTrace (System. err);} finally {System. out. println (" finally statement .. ");} // Code after System. out. println (" try external code... ") ;}}Note: 1. try means to execute the statements in the try. If an Exception is thrown inside the try, the catch part will be executed, as well as the statements outside the try. 2. If an Error occurs inside the try, it indicates an Error. The subsequent statements are not executed, and the catch cannot be captured. 3. however, if there is a finally block, finally will almost certainly be executed, but here there is a sequential problem, the finally block and print exception stack will be executed in another thread, so the order of printing is interesting. 4. If you execute System. exit (0) in try or catch, finally will not be executed. In addition, whether it is Error or return, finally blocks will be executed. 5. You are advised to copy the code or write a copy of the code for execution.

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.