package dividedbyzero;import java.util.InputMismatchException;import java.util.Scanner;/**
*
* @author HP
*/public class Dividedbyzero {
public static int quo(int num,int denum)
throws ArithmeticException
{
return num/denum;
}
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
boolean conlop=true;
do{
try{
System.out.print("please enter integer");
int num=obj.nextInt();
System.out.print("please inter");
int denum=obj.nextInt();
int result=quo(num,denum);
System.out.printf("%nRESULT : %d /%d = %d%n",num,denum,result);
conlop=false;
}
catch(InputMismatchException | ArithmeticException a){
System.err.printf("%n Exception : %s%n",a);
obj.nextLine();
System.out.printf("you mustt num please enter again");
}
}while(conlop);
}}
3 回答

慕妹3242003
TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊
我相信你正在尋找在單個(gè)catch塊中處理兩個(gè)不同異常的東西,它可以像這樣實(shí)現(xiàn),希望你知道可能發(fā)生的所有已檢查異常。
catch(Exception ex){ if(ex instanceOf InputMismatchException ) //Do 1.... else if(ex instanceOf ArithmeticException) //Do 2... else throw ex; }

德瑪西亞99
TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
你能行的。它會(huì)捕獲錯(cuò)誤。您可能想要使用不同的catch塊。在您的程序中,當(dāng)捕獲ArithmeticException時(shí),將向InputMismatchException輸出與用戶相同的消息。此外,我們希望從更具體的異常類型轉(zhuǎn)變?yōu)椴惶唧w的異常類型。
catch(ArtithmeticException e){
//some code
}
catch(Exception e){
//some code
}
在這種情況下,我會(huì)在第一個(gè)catch中使用InputMismatchException,然后捕獲ArithmeticException。
添加回答
舉報(bào)
0/150
提交
取消