标签:
实验四 异常处理
1.建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。
for(inti=0;i<4;i++){ intk; switch(i){ case0:intzero=0;k=911/zero;break; case1:intb[]=null;k?=b[0];break; case2:int c[]=new int[2];k=c[9];break; case3:charch="abc".charAt(99);break; } } package Exception; public class TestException { public static void main(String[] args){ for(int i=0;i<4;i++){ try{ int k; switch(i){ case 0: int zero=0;k=911/zero;break; case 1: int b[]=null;k=b[0];break; case 2: int c[]=new int[2];k=c[9];break; case 3: char ch="abc".charAt(99);break; } } catch(ArithmeticException e){ System.out.println(e.getMessage()); } catch(java.lang.NullPointerException e){ System.out.println(e.getMessage()); } catch(ArrayIndexOutOfBoundsException e){ System.out.println(e.getMessage()); } catch(java.lang.StringIndexOutOfBoundsException e){ System.out.println(e.getMessage()); } } } }
运行结果:
/ by zero
null
9
String index out of range: 99
2.建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),表示存入银行100元,当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。
提示: InsufficientFundsException,NagativeFundsException为自定义的类,分别产生余额不足异常和取款为负数异常,需继承Exception类。
通过输出结果了解java异常的产生,并将该Java程序放在exception包中。
package Exception;//注解先建立一个package,然后在包中建立各种类
public class InsufficientFundsException extends Exception{
String s1;
InsufficientFundsException(String t){
s1=t;
}
public String getMassage1(){
return s1;
}
}
package Exception;
public class NagativeFundsException extends Exception{
String s;
NagativeFundsException(String t){
s=t;
}
public String getMassage(){
return s;
}
}
package Exception;
public class Bank extends Exception{
static double ba=0;
Bank(double r){
ba=ba+r;
}
void withDrawal(double dAmount) throws InsufficientFundsException,NagativeFundsException{
if(dAmount>ba) throw new InsufficientFundsException("取款余额不足");
else if(dAmount<0) throw new NagativeFundsException("取款为负数");
else
System.out.print("银行里还剩余:"+(ba-dAmount));
}
}
package Exception;
import java.util.*;
public class A{
public static void main(String args[] ){
Bank b=new Bank(100);
Scanner sc=new Scanner(System.in);
try {
System.out.println("请输入一个数");
b.withDrawal(sc.nextInt());
}
catch(InsufficientFundsException e){
System.out.println(e.getMassage1());
}
catch(NagativeFundsException e){
System.out.println(e.getMassage());
}
}
}
运行结果:
请输入一个数
80
银行里还剩余:20.0
for(inti=0;i<4;i++){
intk;
switch(i){
case0:intzero=0;k=911/zero;break;
case1:intb[]=null;k?=b[0];break;
case2:int c[]=new int[2];k=c[9];break;
case3:charch="abc".charAt(99);break;
}
}
package Exception;
public class TestException {
public static void main(String[] args){
for(int i=0;i<4;i++){
try{
int k;
switch(i){
case 0: int zero=0;k=911/zero;break;
case 1: int b[]=null;k=b[0];break;
case 2: int c[]=new int[2];k=c[9];break;
case 3: char ch="abc".charAt(99);break;
}
}
catch(ArithmeticException e){
System.out.println(e.getMessage());
}
catch(java.lang.NullPointerException e){
System.out.println(e.getMessage());
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println(e.getMessage());
}
catch(java.lang.StringIndexOutOfBoundsException e){
System.out.println(e.getMessage());
}
}
}
}
运行结果:
/ by zero
null
9
String index out of range: 99
标签:
原文地址:http://www.cnblogs.com/hanruyue/p/5901606.html