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)