Java-10.6 finally (1)-Features

Source: Internet
Author: User

Java-10.6 finally (1)-Features

In this chapter, we will discuss finally.

Feature: finally can always be executed.

 

package com.ray.ch10;public class Test {private void test() throws Exception {throw new Exception();}public static void main(String[] args) {try {new Test().test();} catch (Exception e) {e.printStackTrace();} finally {System.out.println(finally);}}}

Output:

 

Java. lang. Exception
At com. ray. ch10.Test. test (Test. java: 6)
At com. ray. ch10.Test. main (Test. java: 11)
Finally

 

Another example:

 

package com.ray.ch10;public class Test {public static void main(String[] args) {int i = 0;while (true) {try {if (i++ == 0) {throw new Exception();}} catch (Exception e) {e.printStackTrace();} finally {System.out.println(finally);if (i == 2) {System.out.println(break);break;}}}}}

Output:

 

Java. lang. Exception
At com. ray. ch10.Test. main (Test. java: 10)
Finally
Finally
Break

 

Summary: This chapter describes the finally features.

 

This chapter is here. Thank you.

 

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.