异常抛出,捕获处理

时间:2021-06-29 18:25:00
 package error;

public class Throwerror {

 public static void main(String[] args) throws IllegalArgumentException
 {    Point point1=new Point (3,1);
      Point point2=new Point (6,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无效参数异常");
         }
 
 }