异常的应用实例

毕老师用电脑讲课.上课中出现的问题:电脑蓝屏,电脑冒烟.对问题进行描述,封装成对象

 1 public class ExceptionTest1 {

 2     public static void main(String[] args) {

 3         Teacher t = new Teacher("毕老师");

 4         try{

 5             t.prelect();

 6         }

 7         catch (WuFaJiangKeException e){

 8             System.out.println("换老师或者放假");

 9         }

10     }

11 }

12 

13 class LanPingException extends Exception{

14     LanPingException(String mrge){

15         super(mrge);

16     }

17 }

18 

19 class MaoYanException extends Exception{

20     MaoYanException(String mrge){

21         super(mrge);

22     }

23 }

24 

25 class WuFaJiangKeException extends Exception{

26     WuFaJiangKeException(String mrge){

27         super(mrge);

28     }

29 }

30 

31 class Computer{

32     private int s = 2;

33     public void run()throws LanPingException,MaoYanException{

34         if (s == 2)

35             throw new LanPingException("蓝屏了");

36         if (s == 3)

37             throw new MaoYanException("冒烟了");

38 

39         System.out.println("电脑运行了");

40     }

41     public void reset(){

42         s = 1;

43         System.out.println("电脑重启");

44     }

45 }

46 

47 

48 class Teacher{

49     private String name;

50     private Computer cmpt;

51 

52     Teacher(String name){

53         this.name = name;

54         cmpt = new Computer();

55     }

56     public void test(){

57         System.out.println("做练习");

58     }

59     public void prelect()throws WuFaJiangKeException{

60         try{

61             cmpt.run();

62         }

63         catch (LanPingException e){

64             cmpt.reset();

65         }

66         catch (MaoYanException e){

67             test();

68             throw new WuFaJiangKeException("课时无法完成"+e.getMessage());

69         }

70         

71         System.out.println("讲课");

72     }

73 }

 

 有一个圆形和长方形,都可以获取面积,对于面积如果出现了非法的数值(如负数和零),视为是获取面积出现问题
问题通过异常来表示    
现有对这个程序进行基本设计

 1 public class ExceptionTest2 {

 2     public static void main(String[] args) {

 3         Rec r = new Rec(3,5);

 4         r.getArea();

 5     }

 6 

 7 }

 8 //把获取面积定义为一个接口,有获取面积的图形就可以实现这个接口

 9 interface Shape{

10     void getArea();//将共性方法提取出来,因为每个图形获取面积的方式都不一样

11 }

12 class NoValueException extends RuntimeException{

13     NoValueException(String message){

14         super(message);

15     }

16 }

17 class Rec implements Shape{

18     private int len,wid;

19     //矩形一初始化就有长和宽

20     Rec(int len,int wid){

21         if(len<=0 || wid<=0)

22             throw new NoValueException("数值错误");//可以直接抛出RuntimeException异常,但是抛出自定义的异常可以自定义异常信息

23                                                     //抛出的是运行时异常,不用再函数上声明,不用去处理它,应该让程序停掉,

24                                                     //因为数值错误的话,计算不了面积,下面的程序就已经没意义了

25         this.len = len;

26         this.wid = wid;

27     }

28 

29     public void getArea(){

30         System.out.println(len*wid);

31     }

32 }

33 class Circle implements Shape{

34     private int redius;

35     public static final double PI = 3.14;

36     Circle(int radius){

37         if(radius<=0)

38             throw new NoValueException("数值错误");

39         this.redius = radius;

40     }

41     public void getArea(){

42         System.out.println(redius*redius*PI);

43     }

44 }

 

你可能感兴趣的:(异常)