例外處理語法
try{
語句 //可能異常語句。
}
catch
(例外型態 例外名稱){
語句2 //捕獲到例外並要進行處理的語句。
}
finally{
語句3 //最後必須執行。
}
範例:
public
class ExceptionTest {
public
static void main( String args[] ) {
int
w,x,y,z;
w=x=y=10;
try
{
z= w/(x-y);
System.out.println( "數值為= " + z );
}
catch(
ArithmeticException e ) {
System.out.println( "除數不可以為0" );
}
finally
{
z = 100;
System.out.println( "數值為 = " + z );
}
}
}
程式結果是:
除數不可以為0
數值為 =100
沒有留言:
張貼留言