package day03;
/*需求:有一个圆形和长方形。都可以获取面积。对于面积如果出现非法的数值,视为获取面积出现问题。问题通过异常来表示。
*/
class NoValueException extends RuntimeException
{
public NoValueException(String msg) {
super();
}
}
//把面积设计成扩展功能;定义成接口;
interface Shape
{
void getArea();
}
//长方形
class Rec implements Shape
{
private int len,wid;
Rec(int len,int wid)//throws NoValueException
{
if(len<=0||wid<=0)
throw new NoValueException("出现非法值");
this.len = len;
this.wid = wid;
}
public void getArea()
{
System.out.println(len*wid);
}
}
//圆形
class Circle implements Shape
{
private int radius;
public static final double PI=3.14;
Circle(int radius)//throws NoValueException
{
if(radius <=0)
throw new NoValueException("非法");
this.radius =radius;
}
public void getArea()
{
System.out.println(radius*radius*PI);
}
}
public class ExceptionDemo6
{
public static void main(String[] args)
{
//当数值不为负数时了正常运行出结果,当为负数是,因为是runtimeException异常,所以程序直接停掉,
Rec r = new Rec(-2,4);
r.getArea();
/*Circle c = new Circle(5);
c.getArea();*/
System.out.println("over");
}
}
为什么我的结果是这样
Exception in thread "main" day03.NoValueException
at day03.Rec.<init>(ExceptionDemo6.java:23)
at day03.ExceptionDemo6.main(ExceptionDemo6.java:61)
应该有我写的中文的非法信息,怎么没有? |
|