x****a 发帖数: 1229 | 1 作业,创建rectangle class, 要求 throw exceptions, 我的code如下:
public class rectangle extends Shape{
public Rectangle ()
{
this(Shape.DEFAULT_SIZE, Shape.DEFAULT_SIZE);
}
public Rectangle (double newLength, double newWidth)
{
this(Rectangle.RECTANGLE_NAME, newLength, newWidth);
}
protected Rectangle (String newName, double newLength, double newWidth)
{
super (newName);
try
{
this.setLength (newLength);
this.setWidth (newWidth);
}
catch (RectangleException re)
{
this.length = Shape.DEFAULT_SIZE;
this.width = Shape.DEFAULT_SIZE;
}
}
public void setLength (double newLength) throws RectangleException
{
if (newLength <=0.0)
throw new RectangleException("VALUE MUST BE POSITIVE.");
else
this.length = newLength;
}
我的rectangleException:
public class RectangleException extends Exception
{
private static final String DEFAULT_MESSAGE = "message goes here";
public RectangleException()
{
this(RectangleException.DEFAULT_MESSAGE);
}
public RectangleException (String message)
{
super(new String(message));
}
显示错误:
no exception of RectangleException can be thrown, an exception type must be
throwable
请问rectangleException 错在哪里呢?初学者,请勿见笑。谢谢
请问 | m****r 发帖数: 6639 | 2 你的exception需要implemnt throwable
或者直接extend 一个其他的 exception
【在 x****a 的大作中提到】 : 作业,创建rectangle class, 要求 throw exceptions, 我的code如下: : public class rectangle extends Shape{ : public Rectangle () : { : this(Shape.DEFAULT_SIZE, Shape.DEFAULT_SIZE); : } : : public Rectangle (double newLength, double newWidth) : { : this(Rectangle.RECTANGLE_NAME, newLength, newWidth);
| x****a 发帖数: 1229 | 3 谢谢你的回复。
我的rectangle class extends Shape class
所以 rectangelException class也要extends ShapeException class, 而不是直接
extends Exception, 我的理解对吗? | m****r 发帖数: 6639 | 4 that's not required for your code to compile. but that is probably wat you
should do.
【在 x****a 的大作中提到】 : 谢谢你的回复。 : 我的rectangle class extends Shape class : 所以 rectangelException class也要extends ShapeException class, 而不是直接 : extends Exception, 我的理解对吗?
| l**b 发帖数: 457 | 5 他的已经extends Exception了吧?
【在 m****r 的大作中提到】 : 你的exception需要implemnt throwable : 或者直接extend 一个其他的 exception
| l**b 发帖数: 457 | 6 Exception本身就是implements了Throwable了的,你extends和implements达到的效果
都是一样的。
我试了你的code,没有你说的问题啊?
Shape.java:
public class Shape {
public static final double DEFAULT_SIZE = 1.0;
private String name;
public Shape(String name) {
this.name = name;
}
}
Rectangle.java:
public class Rectangle extends Shape {
public static final String RECTANGLE_NAME = "Rectangle";
private double length;
private double width;
public Rectangle ()
{
this(Shape.DEFAULT_SIZE, Shape.DEFAULT_SIZE);
}
public Rectangle (double newLength, double newWidth)
{
this(Rectangle.RECTANGLE_NAME, newLength, newWidth);
}
protected Rectangle (String newName, double newLength, double newWidth)
{
super (newName);
try
{
this.setLength(newLength);
this.setWidth(newWidth);
}
catch (RectangleException re)
{
this.length = Shape.DEFAULT_SIZE;
this.width = Shape.DEFAULT_SIZE;
}
}
public void setWidth(double newWidth) throws RectangleException {
if (newWidth <= 0.0) {
throw new RectangleException("VALUE MUST BE POSITIVE.");
} else {
this.width = newWidth;
}
}
public void setLength (double newLength) throws RectangleException {
if (newLength <=0.0)
throw new RectangleException("VALUE MUST BE POSITIVE.");
else
this.length = newLength;
}
}
RectangleException.java:
public class RectangleException extends Exception {
private static final long serialVersionUID = 1L;
private static final String DEFAULT_MESSAGE = "message goes here";
public RectangleException() {
this(RectangleException.DEFAULT_MESSAGE);
}
public RectangleException (String message) {
super(new String(message));
}
}
【在 x****a 的大作中提到】 : 谢谢你的回复。 : 我的rectangle class extends Shape class : 所以 rectangelException class也要extends ShapeException class, 而不是直接 : extends Exception, 我的理解对吗?
|
|