java异常可以随便处理_关于java异常处理的修改

最近正在学异常处理,写了这么一段代码

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");

}

}

}

请帮忙修改下,最好再加上注释。

非常感谢!

展开

全部

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值