最近正在学异常处理,写了这么一段代码
class table {
public class TooFewLegsError extends Exception
{
private int legs;
private int sides;
最近正在学异常处理,写了这么一段代码
class table {
public class TooFewLegsError extends Exception
{
private int legs;
private int sides;
public TooFewLegsError(int l){legs=l;sides=s;}
public int getLegs(){return legs;}
public int getSides(){return sides;}
}
public class ZeroSideError extends Exception
{
public ZeroSideError(){}
}
public table()
{
int legs;
int sides;
}
public table(int l,int s) throws TooFewLegsError, ZeroSideError
{
if(l<3)
throw new TooFewLegsError(l);
else if(s==0)
throw new ZeroSideError();
else
legs=l;
sides=s;
}
public void setLegs(int l) throws TooFewLegsError
{
if(l<3)
throw new TooFewLegsError(l);
}
public void setSides(int s) throws ZeroSideError
{
if(s==0)
throw new ZeroSideError();
}
public class ClassRoomTable extends table
{
public class IrregularShapeError extends Exception {
public IrregularShapeError() {}
}
private int shape;
public ClassRoomTable(int shape1) throws IrregularShapeError
{
if(shape==4)
throw new IrregularShapeError();
}
public void setShape(int shape1) throws IrregularShapeError
{
if(shape==4)
throw new IrregularShapeError();
}
}
public static void main(String args[])
{
try {
ClassRoomTable p = new ClassRoomTable(2);
p.setShape(3);
p.setSides(-2);
p.setLegs(0);
}
catch (ClassRoomTable.IrregularShapeError n) {
System.out.println(
"不存在不规则形状的桌子.");
}
catch (table.TooFewLegsError n) {
System.out.println("桌子腿出错. " + n.getLegs()
+ "条腿的桌子不存在");
}
catch (table.ZeroSideError n) {
System.out.println(
"形状不能为0");
}
}
}
请帮忙修改下,最好再加上注释。
非常感谢!
展开
全部