异常作业01(2018.08.19)
1、写一个方法void triangle(int a,int b,int c), 判断三个参数是否能构成一个三角形。 如果不能则抛出异常IllegalArgumentException (100分)
1 class IllegalArgumentException extends RuntimeException{ 2 public IllegalArgumentException(){} 3 public IllegalArgumentException(String msg){ 4 super(msg); 5 } 6 } 7 public class Exception_01 { 8 public void triangle(int a,int b,int c) throws IllegalArgumentException{ 9 if(a+b<=c || a-b>=c){ 10 IllegalArgumentException i = new IllegalArgumentException(a+","+b+","+c+"不能构成一个三角形"); 11 throw i; 12 }else{ 13 System.out.print("可以组成一个三角形!"); 14 } 15 } 16 public static void main(String [] agre) throws IllegalArgumentException{ 17 new Exception_01().triangle(3,4,10);//传入三个数 18 } 19 }
运行结果:
1 Exception in thread "main" Exception_and_Multithreading.IllegalArgumentException: 3,4,10不能构成一个三角形 2 at Exception_and_Multithreading.Exception_01.triangle(Exception_01.java:12) 3 at Exception_and_Multithreading.Exception_01.main(Exception_01.java:19)