JAVA Handler exception (try, catch, finally), (THORWS)

Source: Internet
Author: User

One, try statement:

try{//here to write the program that may appear abnormal}

catch (Exception e) {//write here if there is an exception how to process the program}

Second, throws statement

Syntax: Function method () throws Exception {}

Three, try statement example:

ImportJava.util.Scanner; Public classindex{ Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); Try{System.out.println ("Please enter a number:"); intA =In.nextint (); System.out.println ("Please enter a number:"); intb =In.nextint (); intc = a%b; System.out.println (The remainder is: "+c); }        Catch(Exception e) {System.out.println ("Input Error"); }        //Exception The meaning of an exception//the following e can write casually, write E again, because this is an objectSYSTEM.OUT.PRINTLN ("program Continues execution"); }}

Iv. examples of throws statements:

//the usage of throws ExceptionImportJava.util.Scanner; Public classindex{ Public Static voidMain (string[] args)throwsException//exception handling code in the program is not processed so feedback here    {        Try{myclass.t (); //call the static method, test the exception, if it is wrong, in the upward feedback, on the top feedback is a try statement, if the statement is not processed, feedback to the main function        }        Catch(Exception e) {System.out.println ("Input Error"); } System.out.println ("Program continues to execute"); }}classmyclass{Static voidT ()throwsException//If this method is wrong, feedback upward{Scanner in=NewScanner (system.in); System.out.println ("Please enter a number:"); intA =In.nextint (); System.out.println ("Please enter a number:"); intb =In.nextint (); intc = a%b; System.out.println (The remainder is: "+c); }}

V. finally

//the use of finallyImportJava.util.Scanner; Public classindex{ Public Static voidMain (string[] args)throwsException {Try{myclass.t (); }        Catch(Exception e) {System.out.println ("Input Error"); }        finally{System.out.println ("Whether or not it is abnormal, I will continue to execute");//finally is the meaning of this, which is used with try        }    }}classmyclass{Static voidT ()throwsException {Scanner in=NewScanner (system.in); System.out.println ("Please enter a number:"); intA =In.nextint (); System.out.println ("Please enter a number:"); intb =In.nextint (); intc = a%b; System.out.println (The remainder is: "+c); }}

JAVA Handler exception (try, catch, finally), (THORWS)

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.