public class Test For public static void mainString

  • Slides: 40
Download presentation
例一: public class Test. For { public static void main(String[] arg) { for(int i=1,

例一: public class Test. For { public static void main(String[] arg) { for(int i=1, j=i+10; i<5; i++){ j=i*2; System. out. println("i="+i+" j="+j); } }

例二、 public class Test. For { public static void main(String[] arg) { for(int i=1,

例二、 public class Test. For { public static void main(String[] arg) { for(int i=1, j=i+10 ; i<5; i++ , j=i*2) System. out. println("i="+i+" j="+j); } }

例三: public class Test. Break { public static void main(String[] arg) { String output="

例三: public class Test. Break { public static void main(String[] arg) { String output=" "; int i; for(i=1; i<=10; i++){ if (i==5) break; output+=i+" "; } output+="n. Broke out of loop at i ="+1; System. out. println(output); } }

例四: public class Test. Continue { public static void main(String[] arg) { String output="

例四: public class Test. Continue { public static void main(String[] arg) { String output=" "; int i; for(i=1; i<=10; i++){ if (i==5) continue; output+=i+" "; } output+="n. Broke out of loop at i ="+1; System. out. println(output); }

以常规方法处理错误 open. Files; if (the. Files. Open) { determine the lenth of the file;

以常规方法处理错误 open. Files; if (the. Files. Open) { determine the lenth of the file; if (got. The. File. Length){ allocate that much memory; if (got. Enough. Memory) { read the file into memory; if (read. Failed) error. Code=-1; else error. Code=-2; }else error. Code=-3; }else error. Code=-4 ; }else error. Code=-5;

用异常的形式处理错误 read-File; { try { open. The. File; determine its size; allocate that much

用异常的形式处理错误 read-File; { try { open. The. File; determine its size; allocate that much memory; close. The. File; }catch(fileopen. Failed) { dosomething; } catch(size. Determine. Failed) {dosomething; } catch(memory. Allocate. Failed){ dosomething; } catch(read. Failed){ dosomething; } catch(file. Close. Failed) { dosomething; } }

4.1.1 异常示例 import java. io. *; class Exception. Demo 1 {   public static void

4.1.1 异常示例 import java. io. *; class Exception. Demo 1 {   public static void main( String args[ ] ) {     File. Input. Stream fis = new File. Input. Stream( "text" );     int b;     while( (b=fis. read())!=-1 ) {      System. out. print( b );       }     fis. close( );    } }//编译时异常

G: javaException. Demo 1. java: 5: unreported exception java. io. File. Not. Found. Exception;

G: javaException. Demo 1. java: 5: unreported exception java. io. File. Not. Found. Exception; must be caught or declared to be thrown { File. Input. Stream fis = new File. Input. Stream( "text" ); ^ G: javaException. Demo 1. java: 7: unreported exception java. io. IOException; must be caught or declared to be thrown while( (b=fis. read())!=-1 ) ^ G: javaException. Demo 1. java: 11: unreported exception java. io. IOException; must be caught or declared to be thrown fis. close( ); ^ 3 errors

class Exception. Demo 2 {   public static void main( String args[ ] ) {

class Exception. Demo 2 {   public static void main( String args[ ] ) {     int a = 0;     System. out. println( 5/a );   } }//运行时异常

常见的异常 • Arithmetic. Exception • Array. Index. Out. Of. Bands. Exception • Array. Store.

常见的异常 • Arithmetic. Exception • Array. Index. Out. Of. Bands. Exception • Array. Store. Exception 如果在使用能够 • IOException • File. Not. Found. Exception 产生异常的方法 而没有捕获和处 • Null. Pointer. Exception 理,将不能通过 • Malformed. URLException 编译 • Number. Format. Exception • Out. Of. Memory. Exception

 try{     . . .    }catch( File. Not. Found. Exception e ){     System. out. println(

 try{     . . .    }catch( File. Not. Found. Exception e ){     System. out. println( e );     System. out. println( "message: "+e. get. Message() );     e. print. Stack. Trace( System. out );    }catch( IOException e ){     System. out. println( e );    }

throws子句中同时可以指明多个异常,之 子句中同时可以指明多个异常 间由逗号隔开。例如:   public static void main(String args[]) throws IOException, Index. Out. Of. Bounds.

throws子句中同时可以指明多个异常,之 子句中同时可以指明多个异常 间由逗号隔开。例如:   public static void main(String args[]) throws IOException, Index. Out. Of. Bounds. Exception { … }

class Throws. Demo 1 { static void procedure() { System. out. println("inside procedure"); throw

class Throws. Demo 1 { static void procedure() { System. out. println("inside procedure"); throw new Illegal. Access. Exception("demo"); } public static void main(String args[]) { procedure(); } }   

1、请问所有的例外类皆继承哪一个类?    A java. io. Exception B java. lang. Throwable C java. lang. Exception

1、请问所有的例外类皆继承哪一个类?    A java. io. Exception B java. lang. Throwable C java. lang. Exception D java. lang. Error

下面的方法是一个不完整的方法,其中的方法 unsafe()会抛出一个IOException,那么在方法的1)处    应加入哪条语句,才能使这个不完整的方法成为一个完 整的方法?   1)   2) { if(unsafe()){//do something…}   3) else if(safe()){//do the

下面的方法是一个不完整的方法,其中的方法 unsafe()会抛出一个IOException,那么在方法的1)处    应加入哪条语句,才能使这个不完整的方法成为一个完 整的方法?   1)   2) { if(unsafe()){//do something…}   3) else if(safe()){//do the other…}   4) } A public IOException method. Name() B public void method. Name() C public void method. Name() throw IOException D public void method. Name() throws IOException E. public void method. Name() throws Exception

如果下列的方法能够正常运行,在控制台上 将显示什么?      public void example(){    try{       unsafe();       System. out. println("Test 1");

如果下列的方法能够正常运行,在控制台上 将显示什么?      public void example(){    try{       unsafe();       System. out. println("Test 1");      }    catch(Safe. Exception e)      {System. out. println("Test 2"); }    finally{System. out. println("Test 3"); }    System. out. println("Test 4");   } A Test 1 B Test 2 C Test 3 D Test 4

3、对main()方法进行修改来捕获procedure()方 法可能产生的异常。 class Throws. Demo 1 { static void procedure() { System. out. println("inside

3、对main()方法进行修改来捕获procedure()方 法可能产生的异常。 class Throws. Demo 1 { static void procedure() { System. out. println("inside procedure"); throw new Illegal. Access. Exception("demo"); } public static void main(String args[]) { procedure(); } }