/*** Exception: Some instructions that cause the program to run in a program * 1. Exception: Compile period * 2. Non-inspected exception: Runtime * Exception Handling Process Analysis: * 1. Once an exception is generated, the system automatically generates an instantiated object of the exception class * 2. If a corresponding Try statement is present, execute, or Exit, and the system reports an error **/ Public classMain { Public Static voidMain (string[] args) {/*try{//}catch (Exception type Object) {//exception-handling Operation}catch (Exception type Object) with potentially unexpected exception Exception processing Operation} ... finally{//exception of the unified export}*/ intA = 100; Scanner input=NewScanner (system.in); Try{ for(inti=0;i<10;++i) { intb =Input.nextint (); intc = A/b; System.out.println (c); } }Catch(ArithmeticException e) {System.out.println ("Arithmetic Operation exception"); E.printstacktrace (); //exception information in the output stack}Catch(Exception e) {System.out.println ("Operation Exception"); }finally{//Purpose: Release of resourcesSystem.out.println ("Finally: execution occurs regardless of whether the statement in the try is an exception"); } div (10,2); } Public Static intDivintAintb) { intRes=0; Try{res=a/b; returnRes//execute this sentence when you're done with the finally thing .}Catch(ArithmeticException e) {e.printstacktrace (); return0; }finally{System.out.println ("End of division operation"); } }}
Public classMain { Public Static intAdd ()throwsinputmismatchexception{//throw, let the superior to deal withScanner input =NewScanner (system.in); Try{ intA =Input.nextint (); intb =Input.nextint (); returnA +C; }Catch(inputmismatchexception e) {//throw new Inputmismatchexception ("There is a mistake."); Throwe; }finally{System.out.println ("All finished."); } } Public Static voidMain (string[] args) {Try{System.out.println (Add ()); }Catch(Inputmismatchexception e) {//superiors to deal withE.printstacktrace (); } }}
Java Note 9__ exception/throw keyword