实验九—抛出异常
Posted zrwhaha
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验九—抛出异常相关的知识,希望对你有一定的参考价值。
package 实验包;
public class error {
public static void main(String[] args) {
point point1=new point(1,2);
point point2=new point(1,2);
point point3=new point(1,2);
rectangle rectangle1=new rectangle(point1,0,0);
triangle t=new triangle(point1,point2,point3);
point pointx[]= new point[]{point1,point2};
polygon polygonx=new polygon(pointx);
}
}
class point{
point(){}
int x,y;
public point(int x,int y)throws IllegalArgumentException
{
this.x=x;
this.y=y;
if(x<0||y<0){
throw new IllegalArgumentException("抛出无效参数,异常");
}
}
}
class rectangle extends point{
public rectangle(point point1,int length,int width)throws IllegalArgumentException
{
if(length<0||width<0){
throw new IllegalArgumentException("抛出无效参数,异常");
}
}
}
class triangle extends point{
public triangle(point point1,point point2,point point3)throws IllegalArgumentException
{
if((point1.x==point2.x)&&(point1.x==point3.x)&&(point1.y==point2.y)&&(point1.y==point3.y)){
throw new IllegalArgumentException("抛出无效参数,异常");
}
}
}
class polygon extends point{
public polygon(point[] points)throws IllegalArgumentException {
if(points.length<3){
throw new IllegalArgumentException("抛出无效参数,异常");
}
}
}
以上是关于实验九—抛出异常的主要内容,如果未能解决你的问题,请参考以下文章