public class ThrowExample { public static void main(String[] args) { try { _isLaw(1,2,3); //是不是三角形 } catch (Exception e) { System.out.println("异常信息:"+e.getMessage()); } } /** * 计算三角形的勾股定律 */ private static void _isLaw(int a,int b,int c) { if (a <=0 || b<=0 || c<=0) { throw new IllegalArgumentException("传入的参数为负数!"); } if (c*c != a*a + b*b) { throw new IllegalArgumentException("不是三角形!"); } System.out.println("是三角形"); }}
Sign in to make a reply
杜甫