The use of the finally Java

Source: Internet
Author: User

Package com.smbea.demo.trycatchfinally;/** * Java finally usage * @author hapday * @ February 5, 2017 @ Morning 12:21:16 */public class Tryc Atchfinallydemo {public static void main (string[] args) {hasexception (); System.out.println ("\ n —————————————————————————————————————————————————————————————————— \ n"); noneexception (); System.out.println ("\ n —————————————————————————————————————————————————————————————————— \ n"); System.out.println (Hasreturnandnoneexception ()); System.out.println ("\ n —————————————————————————————————————————————————————————————————— \ n"); System.out.println (Hasreturnandhasexception ());}  /** * Has an exception occurs */public static void Hasexception () {int divisor = 10;int Dividend = 0;try {System.out.println (divisor + "/" + dividend + "=" + (divisor/dividend));} catch (Exception e) {System.out.println ("The code here only executes after an exception has occurred ..."); E.printstacktrace (); finally {System.out.println ("The code here always executes, regardless of whether there is an exception or if a return statement appears in the try block, is unaffected ...");} /** * No exception occurs */public static void Noneexception () {int divisor = 10;int Dividend = 10;try {System.out.println (divisor + "/" + dividend + "=" + (divisor/dividend));} catch (Exception e) {System.out.println ("The code here only executes after an exception has occurred ..."); E.printstacktrace (); finally {System.out.println ("The code here always executes, regardless of whether there is an exception or if a return statement appears in the try block, is unaffected ...");} /** * Try block with return statement but no exception * @return */@SuppressWarnings ("finally") public static int hasreturnandnoneexception () {int di  Visor = 10;int Dividend = 10;try {System.out.println (divisor + "/" + dividend + "=" + (divisor/dividend));d ivisor *= 2; System.out.println ("divisor =" + divisor); return divisor;} catch (Exception e) {System.out.println ("The code here only executes after an exception has occurred ..."); E.printstacktrace (); finally {System.out.println ("The code here always executes, regardless of whether there is an exception or if a return statement appears in the try block, is unaffected ...");d ivisor *= 3; System.out.println ("divisor =" + divisor); return divisor;}} /** * Try block with return statement but exception * @return */@SuppressWarnings ("finally") public static int hasreturnandhasexception () {int div Isor = 10;int Dividend = 0;try {System.out.println (divisor + "/" + dividend + "=" + (divisor/dividend));d ivisor *= 2; System.out.println ("divisor =" + divisor); return divisor;} catch (Exception e) {System.out.println ("The code here only executes after an exception has occurred ..."); E.printstacktrace (); finally {System.out.println ("The code here always executes, regardless of whether there is an exception or if a return statement appears in the try block, is unaffected ...");d ivisor *= 3; System.out.println ("divisor =" + divisor); return divisor;}}}

  

The use of the finally Java

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.