实验九 异常的抛出,捕获并处理

1.源程序

package err;

public class err {
public static void main(String[] args) throws IllegalArgumentException
{ Point point1=new Point (25,1);
Point point2=new Point (46,2);
Point point3=new Point (9,3);
new Rectangle(point1, 12,3);
new Triangle(point1,point2,point3);
Point[] point= {point1,point2};
new Polygon(point);
}

}

class Point
{ public Point() {};
public int x;
public int y;
public Point(int x,int y)
{ this.x=x;
this.y=y;

if(x<0||y<0)
throw new IllegalArgumentException("Point中无效参数异常");
}
}
class Rectangle extends Point
{
public int length,width;
public Rectangle(Point point1,int length,int width) {
this.length=length;
this.width=width;
if(length<0||width<0)
throw new IllegalArgumentException("Rectangle无效参数异常");
}
}
class Triangle extends Point 
{

public Triangle(Point point1,Point point2,Point point3) {
if(point1.x/point1.y==point2.x/point2.y&&point2.x/point2.y==point3.x/point3.y)
throw new IllegalArgumentException("Triangle无效参数异常");
}
}

class Polygon extends Point
{ int i=0;
public Polygon(Point[] points)
{ i=points.length;
if(i<=2)
throw new IllegalArgumentException("Polygon无效参数异常");
}

}

2.实验结果

 

 

 3.心得体会

       此实验对我来说难度较大,对异常的捕获的概念比较模糊。通过向同学请教了解了一些,不过还是没有熟练掌握,以后好好改进。

 

posted @ 2019-05-27 12:42  武智琳L  阅读(169)  评论(0编辑  收藏  举报