一道關(guān)于java異常處理的題目
1、建立exception包,編寫TestException.java程序,主方法中有以下代碼,確定其中可能出現(xiàn)的異常,進行捕獲處理。
public class YiChang {
public static void main(String[] args){
for(int i=0;i<4;i++){
int k;
switch(i){
case 0: int zero=0;
try{
k=911/zero;
}catch(ArithmeticException e){
System.out.println("出現(xiàn)算數(shù)異常!");
}
break;
case 1:
try{
int b[]=null;
k = b[0];
}catch(NullPointerException e){
System.out.println("出現(xiàn)空指針異常!");
}
break;
case 2:
int c[]=new int[2];
try{
k=c[9];
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("出現(xiàn)數(shù)組序號溢出!");
}
break;
case 3:
try{
char ch="abc".charAt(99);
}catch(StringIndexOutOfBoundsException e){
System.out.println("出現(xiàn)數(shù)據(jù)類型轉(zhuǎn)換異常!");
}
break;
}
}
}
}

2、建立exception包,建立Bank類,類中有變量double balance表示存款,Bank類的構(gòu)造方法能增加存款,Bank類中有取款的發(fā)方法withDrawal(double dAmount),當(dāng)取款的數(shù)額大于存款時,拋出InsufficientFundsException,取款數(shù)額為負數(shù),拋出NagativeFundsException,如new Bank(100),表示存入銀行100元,當(dāng)用方法withdrawal(150),withdrawal(-15)時會拋出自定義異常。
public class InsufficientFundsException extends Exception {
public String getMessage(){
return "您的余額不足!";
}
}
public class NagativeFundsException extends Exception{
public String getMessage(){
return "取款金額不能為負數(shù)!";
}
}
public class Bank {
private static double balance;
Bank(){
};
Bank(double balance){
this.balance=balance;
}
public static void withDrawal(double dAmount) throws InsufficientFundsException,NagativeFundsException{
if(dAmount>balance){
throw new InsufficientFundsException();
}
if(dAmount<0){
throw new NagativeFundsException();
}
}
public static void main(String[] args){
Bank b=new Bank(100);
System.out.println("我有"+balance+"元存款!");
try{
withDrawal(150);
}catch(InsufficientFundsException | NagativeFundsException e){
e.printStackTrace();
}
try{
withDrawal(-15);
}catch(NagativeFundsException |InsufficientFundsException e){
e.printStackTrace();
}
}
}

一道關(guān)于一道關(guān)于java異常處理的題目就給大家介紹這么多,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的,在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
spring boot使用properties定義短信模板的方法教程
這篇文章主要給大家介紹了關(guān)于spring boot使用properties定義短信模板的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
java調(diào)用webService接口的代碼實現(xiàn)
本文主要介紹了java調(diào)用webService接口的代碼實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Spring Boot利用@Async異步調(diào)用:使用Future及定義超時詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot利用@Async異步調(diào)用:使用Future及定義超時的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2018-05-05
深入理解Java8新特性之Stream API的創(chuàng)建方式和中間操作步驟
Stream是Java8的一大亮點,是對容器對象功能的增強,它專注于對容器對象進行各種非常便利、高效的 聚合操作(aggregate operation)或者大批量數(shù)據(jù)操作。Stream API借助于同樣新出現(xiàn)的Lambda表達式,極大的提高編程效率和程序可讀性,感興趣的朋友快來看看吧2021-11-11
Java如何使用HTTPclient訪問url獲得數(shù)據(jù)
這篇文章主要介紹了Java使用HTTPclient訪問url獲得數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
完美解決idea創(chuàng)建文件時,文件不分級展示的情況
這篇文章主要介紹了完美解決idea創(chuàng)建文件時,文件不分級展示的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02

