public class ExceptionDemo {
public static void main(String[] args) {
//main方法接收的值在右上角編譯按鈕旁邊的下拉框里的第一個(gè)選項(xiàng)
// 在構(gòu)造與項(xiàng)目塊里面的第二行里面輸入傳入的參數(shù)
int result= 0;
try {
int i=Integer.parseInt(args[0]);
//Integer是包裝類(lèi)(每一個(gè)基礎(chǔ)的數(shù)據(jù)類(lèi)型都有一個(gè);類(lèi))
// 進(jìn)行拆包:將Integer直接傳給一個(gè)int變量
// 通過(guò)Integer將args[0]的類(lèi)型變成Integer類(lèi)型
int j=Integer.parseInt(args[1]);
result = ecm(i,j);
//定義一個(gè)ecm()方法
} catch (NumberFormatException e) {
System.out.println("數(shù)據(jù)轉(zhuǎn)換錯(cuò)誤!");
e.printStackTrace();
}catch (ExceptionDefine e) {
e.printStackTrace();
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("參數(shù)太少了!");
}catch (ArithmeticException e){//捕捉除數(shù),如果為零,就輸出異常
System.out.println("除數(shù)為0了!");
}
System.out.println(result);
}
public static int ecm(int a,int b) throws ExceptionDefine{
int c=0;
if(a<0||b<0)//結(jié)果不能為負(fù)數(shù),如果a,或b小于0拋出一個(gè)自定義異常
throw new ExceptionDefine("輸入負(fù)數(shù)了,不行,重新輸入!");
else
c=a/b;
return c;
}
}
自定義的異常類(lèi)import javax.management.RuntimeOperationsException;
public class ExceptionDefine extends RuntimeException {
static final long serialVersionUID = -90745766939L;
//序列號(hào),用于匹配拋出的異常,可以通過(guò)按ctrl再點(diǎn)擊繼承的異常父類(lèi),在里面復(fù)制
//空異常
public ExceptionDefine() {
}
//一個(gè)輸出帶有信息的異常,選擇Massage String 異常
public ExceptionDefine(String message) {
//調(diào)用super里面的方法輸出并打印異常的信息
super(message);
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站題目:異常練習(xí)題(Java)-創(chuàng)新互聯(lián)
瀏覽路徑:http://www.chinadenli.net/article16/gsigg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、電子商務(wù)、網(wǎng)站導(dǎo)航、ChatGPT、標(biāo)簽優(yōu)化、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)