Redian新闻
>
初学者try catch in constructor 问题
avatar
初学者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 错在哪里呢?初学者,请勿见笑。谢谢
请问
avatar
m*r
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);

avatar
x*a
3
谢谢你的回复。
我的rectangle class extends Shape class
所以 rectangelException class也要extends ShapeException class, 而不是直接
extends Exception, 我的理解对吗?
avatar
m*r
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, 我的理解对吗?

avatar
l*b
5
他的已经extends Exception了吧?

【在 m****r 的大作中提到】
: 你的exception需要implemnt throwable
: 或者直接extend 一个其他的 exception

avatar
l*b
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, 我的理解对吗?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。