package fault_Finally;
interface Shape{
public void getArea()throws Exception;
}
class Rec implements Shape{
private double length,width;
Rec(double length,double width)
{
if(length<0||width<0){
throw new AreaException("负数参数");
}
this.length=length;
this.width=width;
}
public void getArea()
{
System.out.println("Area="+length*width);
}
}
class Cir implements Shape{
private double radius;
public static final double PI=3.1415926;
Cir(double radius)
{
if(radius<0){
throw new AreaException("负数参数");
}
this.radius=radius;
}
public void getArea()
{
System.out.println("Area="+PI*radius*radius);
}
}
//---------------------------------------------------
class AreaException extends RuntimeException{
AreaException(String msg){
super(msg);
}
}
//---------------------------------------------------
public class Practise {
public static void main(String[] args){
Rec rr;
//try{
rr=new Rec(25.0,30.0);
/*}
catch(AreaException e){
throw e;
}
*/
rr.getArea();
Cir cc=new Cir(-2);
cc.getArea();
}
}
java_异常处理
最新推荐文章于 2022-07-17 15:52:28 发布