1、源代碼函數
package err;this
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、結果blog
3、心得io
一、Java中的全部異常都是由Throwable類的子類生成的對象,全部的異常類都是Throwable類的子類或子類的子類。Throwable類是Object類的直接子類,Error類和Exception類是Throwable類的兩個直接子類。class
二、throw 異常對象im
三、拋出異常能夠直接在主函數中拋出,也能夠在類中直接拋出 異常