例外就是在程序的运行过程中所发生的异常事件,它中断指令的正常执行。Java中提供了一种独特的处理例外的机制,通过例外来处理程序设计中出现的错误。
【例1-1】
import java.io.*;
class ExceptionDemo1{
public static void main( String args[ ] ){
FileInputStream fis = new FileInputStream( "text" );
int b;
while( (b=fis.read())!=-1 ){
System.out.print( b );
}
fis.close( );
}
}
查看编译结果
【例1-2】
class ExceptionDemo2{
public static void main( String args[ ] ){
int a = 0;
System.out.println( 5/a );
}
}
查看运行结果
|