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

该博客记录了Java实验,展示了实验源码和结果,出现了 IllegalArgumentException 异常。通过此次实验,作者了解到异常处理机制的必要性及处理措施,还给出了转载来源。

实验源码:

 1 package 实验9;
 2 
 3 public class Shiyan9 {
 4 public static void main(String[] args) {
 5 point p=new point(1,3);
 6 point p1=new point(1,2);
 7 point p2=new point(1,1);
 8 rectangle r=new rectangle(p,5,6);
 9 triangle t=new triangle(p,p1,p2);
10 }
11 
12 }
13 class point {
14 public int x,y;
15 public point() {}
16 public point(int x,int y)throws IllegalArgumentException
17 {
18 this.x=x;
19 this.y=y; 
20 
21 if(x<0||y<0)
22 throw new IllegalArgumentException("参数无效");    
23 }
24 }
25 class rectangle extends point{
26 public int width,length;
27 //public point point1(3,6);
28 public rectangle(point point1,int length,int width)throws IllegalArgumentException
29 {
30 
31 this.length=length;
32 this.width=width;
33 if(length<0||width<0)
34 throw new IllegalArgumentException("参数无效");
35 }
36 } 
37 class triangle extends point{
38 public triangle(point point1,point point2,point point3)throws IllegalArgumentException
39 {
40 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)
41 throw new IllegalArgumentException("参数无效");
42 }
43 }

实验结果:

Exception in thread "main" java.lang.IllegalArgumentException: 参数无效
at 实验9.triangle.<init>(Shiyan9.java:42)
at 实验9.Shiyan9.main(Shiyan9.java:10)

 

实验心得:

通过本次实验,了解异常处理机制的必要性和异常处理的措施。

转载于:https://www.cnblogs.com/itsRes/p/10927587.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值