m0_56994570 2021-06-05 21:17 采纳率: 100%
浏览 36
已采纳

用Java写对异常进行捕获和处理

 

  • 写回答

5条回答 默认 最新

  • 关注
    
    public class TriangleTest {
    
    	int a;
    	int b;
    	int c;
    	public void triangle(int a,int b,int c) throws IllegalArgumentException {
    		this.a = a;
    		this.b = b;
    		this.c = c;
    		if(a+b<=c || a+c<=b || b+c<=a) {
    			throw new IllegalArgumentException("参数错误");
    		}
    	}
    	public double getArea(int a,int b,int c) {
    		
    		double p = (a+b+c)/2;
    		
    		return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    		
    	}
    public double getArea() {
    		
    		double p = (a+b+c)/2;
    		
    		return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    		
    	}
    	public static void main(String[] args) {
    		try {
    			TriangleTest tt = new TriangleTest();
    			tt.triangle(3, 4, 5);
    			System.out.println("三角形面积:"+tt.getArea());
    		}catch(IllegalArgumentException e) {
    			System.out.println(e.getMessage());
    		}finally {
    			System.out.println("运行结束");
    		}
    	}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?