本帖最后由 lucy198921 于 2013-4-2 18:45 编辑
- class RuntimeExceptionDemo
- {
- public int getArea(int width,int heigth)
- {
- if (width<=0||heigth<=0)
- {
- throw new RuntimeException("数据非法,矩形不存在");
- }
- return width*heigth;
- }
- public int getArea1(int width,int heigth)
- {
- if (width<=0||heigth<=0)
- {
- throw new RuntimeException("数据非法,矩形不存在");
- }
- return width*heigth;
- }
- }
- class ExceptionDemo2
- {
- public static void main(String[] args)
- {
- RuntimeExceptionDemo re=new RuntimeExceptionDemo();
- int area=0;
- int area1=0;
- area=re.getArea(2,-8);
- area1=re.getArea1(2,8);
- boolean b= compare(area,area1);
- System.out.println("b="+b);
- }
- public static boolean compare(int area, int area1)
- {
- return area==area1;
- }
- }
复制代码 |