HUST IBM Computer Tech Center public class Example






![例程 HUST & IBM Computer Tech. Center public class Example. Of. Exception { String[] 例程 HUST & IBM Computer Tech. Center public class Example. Of. Exception { String[]](http://slidetodoc.com/presentation_image_h/ecc0d299983e31921a19dbfb495b1fff/image-7.jpg)
例程 HUST & IBM Computer Tech. Center public class Example. Of. Exception { String[] lines = {"The first line", "The second line", "The last line"}; public static void main (String[] args) { Example. Of. Exception eoe = new Example. Of. Exception(); eoe. method. A(); System. out. println("Program finished. "); } void method. A() { method. B(); } The{first line void method. B() method. C(); The second line The last line } Exception in thread "main" void method. C() { java. lang. Array. Index. Out. Of. Bounds. Exception: 3 for (int i=0; at i<4; i++) System. out. println (lines[i]); Example. Of. Exception. method. C(Example. Of. Exception. java: 16) } at Example. Of. Exception. method. B(Example. Of. Exception. java: 12) } at Example. Of. Exception. method. A(Example. Of. Exception. java: 9) 华中科技大学IBM技术中心


Exception类 HUST & IBM Computer Tech. Center v. Exception的子类表示了不同类型的异常, 例如Runtime. Exception表示运行时异常, 而IOException表示I/O问题引起的异常。 v这些子类也可以被继承以对不同类型的异 常进行细分,如Runtime. Exception还可细 分为Null. Pointer. Exception、 Arithmetic. Exception等;IOException还 可细分为File. Not. Found. Exception、 EOFException等。 华中科技大学IBM技术中心

常见的异常类 HUST & IBM Computer Tech. Center v Arithmetic. Exception v Array. Index. Out. Of. Bands. Exception v IOException v File. Not. Found. Exception v Null. Pointer. Exception v Number. Format. Exception 华中科技大学IBM技术中心


常见的错误类 HUST & IBM Computer Tech. Center v No. Class. Def. Found. Error v Out. Of. Memory. Error v Virtual. Machine. Error 华中科技大学IBM技术中心







抛出异常 HUST & IBM Computer Tech. Center v在下面的例子里,对于父类Super. Class而 言,类Sub. Class. A是正确的子类,而 Sub. Class. B则是错误的。 class Super. Class{ public super. Method() throws EOFException { … } } class Sub. Class. A extends Super. Class{ //正确 public super. Method() { … } } class Sub. Class. B extends Super. Class{ //错误 public super. Method() throws File. Not. Found. Exception{ … } 华中科技大学IBM技术中心 }



![example HUST & IBM Computer Tech. Center public class Example. Of. Exception { String[] example HUST & IBM Computer Tech. Center public class Example. Of. Exception { String[]](http://slidetodoc.com/presentation_image_h/ecc0d299983e31921a19dbfb495b1fff/image-23.jpg)
example HUST & IBM Computer Tech. Center public class Example. Of. Exception { String[] lines = {"The first line", "The second line", "The last line"}; public static void main (String[] args) { Example. Of. Exception eoe = new Example. Of. Exception(); eoe. method. A(); System. out. println("Program finished. "); }. . . void method. C() { for (int i=0; i<4; i++) { try { System. out. println (lines[i]); } catch (Array. Index. Out. Of. Bounds. Exception e) { System. out. println("Re-setting Index Value"); } } 华中科技大学IBM技术中心







example HUST & IBM Computer Tech. Center public class Try. Catch. Finally{ static void Proc( int sel ){ System. out. println("----- In Situation"+sel+" -----"); try{ if( sel==0 ){ System. out. println("no Exception caught"); return; }else if( sel==1 ){ int i=0; int j=4/i; }else if( sel==2 ){ int i. Array[]=new int[4]; i. Array[10]=3; } 华中科技大学IBM技术中心

example HUST & IBM Computer Tech. Center }catch( Arithmetic. Exception e ){ System. out. println("Catch "+e); }catch( Array. Index. Out. Of. Bounds. Exception e ){ System. out. println("Catch "+e. get. Message()); }catch( Exception e ){ System. out. println("Will not be executed"); 程序运行结果: }finally{ ----- In Situation 0 ----System. out. println("in no Exception Proc caught finally"); } in Proc finally } ----- In Situation 1 ----public static void main( String args[] ){ Catch java. lang. Arithmetic. Exception: / by zero Proc( 0 ); in Proc finally Proc( 1 ); ----- In Situation 2 ----Proc( 2 ); Catch 10 } in Proc finally } 华中科技大学IBM技术中心


example HUST & IBM Computer Tech. Center public class Throws. Exception 1{ static void Proc(int sel) throws Array. Index. Out. Of. Bounds. Exception { System. out. println("-----In Situation"+sel+"----"); if(sel==0){ System. out. println("no Exception caught"); return; }else if(sel==1){ int i. Array[]=new int[4]; i. Array[10]=3; } } 华中科技大学IBM技术中心
![example HUST & IBM Computer Tech. Center public static void main(String args[]){ try{ Proc(0); example HUST & IBM Computer Tech. Center public static void main(String args[]){ try{ Proc(0);](http://slidetodoc.com/presentation_image_h/ecc0d299983e31921a19dbfb495b1fff/image-34.jpg)
example HUST & IBM Computer Tech. Center public static void main(String args[]){ try{ Proc(0); Proc(1); }catch(Array. Index. Out. Of. Bounds. Exception e){ System. out. println("Catch "+e); }finally{ System. out. println("in Proc finally"); } } } 程序运行结果: ----- In Situation 0 ----no Exception caught ----- In Situation 1 ----Catch java. lang. Array. Index. Out. Of. Bounds. Exception: 10 in Proc finally 华中科技大学IBM技术中心


example HUST & IBM Computer Tech. Center class My. Exception extends Exception{ private int detail; My. Exception( int a ){ detail = a; } public String to. String( ){ return "My. Exception "+detail; } } 华中科技大学IBM技术中心

example HUST & IBM Computer Tech. Center public class Exception. Demo{ static void compute(int a) throws My. Exception { System. out. println("called compute("+a+")"); if( a>10 ) throw new My. Exception(a); System. out. println("normal exit"); } public static void main( String args[] ){ try{ compute( 1 ); compute( 20 ); } catch( My. Exception e 程序运行结果: ){ called"+e); compute(1) System. out. println("Caught } normal exit } called compute(20) } Caught My. Exception 20 华中科技大学IBM技术中心




两段代码的比较 HUST & IBM Computer Tech. Center 代码1: try { int n = Input. Reader. input. Integer(请输入一个整数"); if (n<100 || n>1) throw new Number. Format. Exception(); }catch (Number. Format. Exception e) { System. out. println("输入范围错误!"); } 代码2: int n = Input. Reader. input. Integer(请输入一个整数"); if (n<100 || n>1) System. out. println("输入范围错误!"); v代码1采用了异常处理方式;代码2则通过对用户输入的分 析避免了异常的使用,提高了代码效率。 华中科技大学IBM技术中心



问题 HUST & IBM Computer Tech. Center public static void cat(File named) { Random. Access. File input = null; String line = null; try { input = new Random. Access. File(named, “r”); while ((line = input. read. Line()) != null { System. out. println(line); } return; } finally { if (input != null) { input. close(); } } } 华中科技大学IBM技术中心
- Slides: 44