2、定义一个circle类,其中有求面积的方法,当半径小于0时,抛出一个用户自定义异常”。 package dai.zzu.edu.cn; import java.util.*; public class circle{ private static double p=3.14; private static double radius; public static void getArea(double radius1)throws AnswerException { if(radius1<0) throw new AnswerException (radius1); System.out.println(p*radius1*radius1); } public static void main(Stringargs[]){ Scanner in=new Scanner(System.in); System.out.println("shuru:"); doubleradius11=in.nextDouble(); try{ getArea(radius11); System.out.println("end"); }catch(AnswerException e){ System.out.println("result is wrong:"+e.getRadius()); e.printStackTrace(); } } } package dai.zzu.edu.cn; import java.util.*; class AnswerException extends Exception { private double radius; public AnswerException(double radius) { this.radius=radius; } public double getRadius() { return radius; } }
|