1 PackageOrg.hanqi.ex;2 3 Public classTestException {4 5 Public Static voidMain (string[] args) {6 7 //Create an exception8 //except for 0 exceptions9 //1. CaptureTen Try One { A inti = 12/10; - - //Open Some resources theString s =NULL; -SYSTEM.OUT.PRINTLN ("Open resource"); - //Use - //If an exception occurs in use +SYSTEM.OUT.PRINTLN ("Use resources"); - s.length (); + //close the resource and release ASYSTEM.OUT.PRINTLN ("Release resource code that you want to be sure to execute"); at } - Catch(ArithmeticException ex) - { - //special handling for specific exceptions -System.out.println ("Caught ArithmeticException exception"); - //Get exception information inString str =ex.getmessage (); - //How to: Display or log logs toSYSTEM.OUT.PRINTLN ("Exception information =" +str); + //output Exception details to the console - ex.printstacktrace (); the System.out.println (); * } $ Panax Notoginseng Catch(NullPointerException ex) - { the //special handling for specific exceptions +System.out.println ("Caught NullPointerException exception"); A //Get exception information theString str =ex.getmessage (); + //How to: Display or log logs -SYSTEM.OUT.PRINTLN ("Exception information =" +str); $ //output Exception details to the console $ ex.printstacktrace (); - System.out.println (); - } the Catch(Exception ex) - {Wuyi //special handling for specific exceptions theSystem.out.println ("Caught exception exception"); - //Get exception information WuString str =ex.getmessage (); - //How to: Display or log logs AboutSYSTEM.OUT.PRINTLN ("Exception information =" +str); $ //output Exception details to the console - ex.printstacktrace (); - System.out.println (); - } A finally//code that is bound to be executed + { theSYSTEM.OUT.PRINTLN ("Release resource code that you want to be sure to execute"); - } $ the Try the { the intj = 12/0; the } - finally in { theSYSTEM.OUT.PRINTLN ("Release resource code that you want to be sure to execute"); the } AboutSYSTEM.OUT.PRINTLN ("program Continues execution"); the } the}
1 PackageOrg.hanqi.ex;2 3 Public classMythrow {4 5 //Put the throws behind the method, specifically declaring that the method is likely to be abnormal6 //The method itself is not processed and given to the caller to handle7 Public intChuintAintbthrowsException8 {9 returnA/b;Ten } One Public voidDiaoyong ()throwsException A { - Try { - inti = Chu (12,10); the}Catch(Exception e) { - //TODO Auto-generated catch block - //e.printstacktrace (); -SYSTEM.OUT.PRINTLN ("I caught an exception" +e.getmessage ()); + //actively throw - Throwe; + } A } at Public voidDiaoyong2 ()throwsException - { - inti = Chu (12,0); - } - Public intTestintAintbthrowsException - { in //Data Validation - if(b==0) to { + //Define yourself -Exception e =NewException ("My custom exception info: The second parameter cannot be equal to 0"); the Throwe; * } $ returnA/b;Panax Notoginseng } - Public Static voidMain (string[] args) the { +Mythrow MT =NewMythrow (); A Try { the Mt.diaoyong (); +}Catch(Exception e) { - //TODO Auto-generated catch block $ e.printstacktrace (); $ } -SYSTEM.OUT.PRINTLN ("program continues to run"); - the Try { -Mt.test (30,0);Wuyi}Catch(Exception e) { the //TODO Auto-generated catch block - //e.printstacktrace (); WuSYSTEM.OUT.PRINTLN ("Exception info:" +e.getmessage ()); - } About } $}
Related Mind Mapping:
What we say in class: anomalies