The concept of exceptions
Classification of exceptions
exception capture and handling
ImportJava.io.*; Public classTestEx { Public Static voidMain (string[] args) {Try { NewTestEx (). F2 (); } Catch(IOException e) {e.printstacktrace (); } /*int[] arr = {1, 2, 3}; System.out.println (arr[2]); try {System.out.println (2/0); } catch (ArithmeticException e) {System.out.println ("system is being maintained, please contact the Administrator"); E.printstacktrace (); } */ //TestEx te = new TestEx (); //TE.M (0); /*try {new TestEx (). m (0); } catch (ArithmeticException ae) {ae.printstacktrace (); SYSTEM.OUT.PRINTLN ("error"); } */FileInputStream in=NULL; Try{ in=NewFileInputStream ("MyFile.txt"); intb; b=In.read (); while(b! =-1) {System.out.print (Char) b); b=In.read (); } } Catch(IOException e) {System.out.println (E.getmessage ()); } Catch(FileNotFoundException e) {e.printstacktrace (); } finally { Try{in.close (); } Catch(IOException e) {e.printstacktrace (); } } } voidMintIthrowsArithmeticException {if(i==0) Throw NewArithmeticException ("Divide by 0"); } voidF ()throwsFileNotFoundException, IOException {fileinputstream in=NewFileInputStream ("MyFile.txt"); intb; b=In.read (); while(b! =-1) {System.out.print (Char) b); b=In.read (); } } voidF2 ()throwsIOException {/*try {f (); } catch (FileNotFoundException e) {System.out.println (E.getmessage ()); } catch (IOException e) {e.printstacktrace (); } */f (); } }
Testex.java
using a custom exception
declaring and Throwing exceptions
J2SE _ Exception Handling