2018年11月7日 星期三

[Java]_ch12.2_拋出例外





[Java]_ch12.2_拋出例外



package YPU;

class Test{

public void div(int a,int b)throws ArithmeticException {

System.out.println(a+"/"+b+"="+(a/b));

}

}



public class A12

{

public static void main(String args[])

{

int A[]=new int [5];

try{

A[10]=100;

}catch(Exception e){

A[0]=100;

System.out.println("[1.Catch]執行,A[0]="+A[0]+" e="+e);

}

finally{

System.out.println("[Finally]必做程式!");

}

try{

int a=5,b=0;

if(b==0)

throw new ArithmeticException ();

else

System.out.println(a+"/"+b+"="+(a/b));

}catch(ArithmeticException e){

System.out.println("[2.Catch]執行, e="+e);

}

try{

Test t1=new Test();

t1.div(10, 0);

}catch(ArithmeticException e){

System.out.println("[3.Catch]執行, e="+e);

}





System.out.println("主程式結束...A[0]="+A[0]);

}

}




沒有留言:

張貼留言

Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...