zoukankan      html  css  js  c++  java
  • 实验九:异常的抛出、捕获并处理

    源代码:

    package yichang;

    public class Yichang {

    public static void main(String[] args) {

    point p1=new point(1,3);
    point p2=new point(1,2);
    point p3=new point(1,1);
    new rectangle(p1,-2,6);
    new triangle(p1,p2,p3);
    point[] point= {p1,p2};
    new Polygon(point);
    }

    }
    class point {
    public int x,y;
    public point() {}
    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 int width,length;

    public rectangle(point point1,int length,int width)throws IllegalArgumentException
    {

    this.length=length;
    this.width=width;
    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.y)-(point2.x-point1.y))+((point2.x-point3.y)-(point3.x-point2.y))+((point3.x-point1.y)-(point3.y-point1.x))==0)
    throw new IllegalArgumentException("无效的参数");
    }
    }
    class Polygon extends point
    { int i=0;
    public Polygon(point[] points)
    { i=points.length;
    if(i<=2)
    throw new IllegalArgumentException("Polygon无效参数异常");
    }

    }

    结果:

    Exception in thread "main" java.lang.IllegalArgumentException: 参数无效
    at yichang.rectangle.<init>(Yichang.java:40)
    at yichang.Yichang.main(Yichang.java:12)

     心得体会:


    通过本次实验我学习到了异常的抛出,捕获,并处理。但在具体输出中还是有些错误,但在同学帮助下理解了异常处理。

  • 相关阅读:
    kubernetes 二进制安装v2
    Istio
    linux的history指令显示时间
    echarts折线图y轴刻度值按照某个值的倍数
    ubuntu下tesseract 4.0安装及参数使用
    chrome 浏览器下载大文件断掉的问题
    强化学习
    第十一篇 -- 2020总结以及2021期待
    字符串时间yyyyMMddHHmmss转成yyyy-MM-dd HH-mm-ss字符串格式
    windows安装mongodb,密码访问
  • 原文地址:https://www.cnblogs.com/Z-js/p/10927655.html
Copyright © 2011-2022 走看看