初学者try catch in constructor 问题# Java - 爪哇娇娃
x*a
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 错在哪里呢?初学者,请勿见笑。谢谢
请问
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 错在哪里呢?初学者,请勿见笑。谢谢
请问