/*
有一个圆形和长方形
都可以获取面积,对于面积如果出现非法的数值,视为是获取面积出现问题。
问题通过异常来表示。
现有对这个程序进行基本设计
*/
interface Shape
{
public void areas();
}
class AreasException extends Exception
{
private String msg;
AreasException(String msg)
{
super(msg);
}
}
class Circle implements Shape
{
private final static double PI=3.14;
private double redius;
Circle(double redius)//throws AreasException
{
if(redius<0)
throw new AreasException("半径为负了");
try
{
this.redius=redius;
}
catch(AreasException e)
{
System.out.println(e.toString()+".......");
}
}
public void areas()
{
System.out.println(redius*redius*PI);
}
}
class Res implements Shape
{
private int width;
private int len;
Res(int width,int len) //throws AreasException
{
if(width<0||len<0)
throw new AreasException("长度未付");
this.width=width;
this.len=len;
}
public void areas()
{
System.out.println(width*len);
}
}
/*
class FuShuException extends RuntimeException
{
FuShuException(String msg)
{
super(msg);
}
}
class Demo
{
public int div(int a,int b) //throws ArithmeticException
{
if(b<0)
throw new FuShuException("。。。。除数为负了");
if(b==0)
throw new ArithmeticException("除数是零了");// 换成new Exception("除数是零了");
return a/b;
}
}*/
class RuntimeExceptionDemo
{
public static void main(String[] args) //throws AreasException
{
Circle c=new Circle(-20);
c.areas();
Res r=new Res(-10,29);
r.areas();
/*Circle c=new Circle(20);
try
{
c.areas();
}
catch (AreasException e)
{
System.out.println(e.toString());
}
Res r=new Res(-10,29);
try
{
r.areas();
}
catch (AreasException e)
{
System.out.println(e.toString());
}*/
}
}
|
|