首页 > 其他 > 详细

自定义异常

时间:2015-06-27 16:07:02      阅读:87      评论:0      收藏:0      [点我收藏+]
 1 package org.zln.exception;
 2 
 3 /**
 4  * Created by coolkid on 2014/12/28 0028.
 5  */
 6 
 7 class LanPingException extends Exception{
 8     LanPingException(String msg){
 9         super(msg);
10     }
11 }
12 
13 class MaoYanException extends Exception{
14     MaoYanException(String msg){
15         super(msg);
16     }
17 }
18 
19 class Computer{
20     private int state = 2;
21     public void run() throws LanPingException, MaoYanException {
22         if (state == 1){
23             throw new LanPingException("电脑蓝屏了");
24         }else if (state == 2){
25             throw new MaoYanException("电脑冒烟了");
26         }
27         System.out.println("电脑运行");
28     }
29 }
30 
31 class Teacher{
32     private String name;
33     private Computer computer;
34 
35     public Teacher(String name) {
36         this.name = name;
37         computer = new Computer();
38     }
39 
40     public void prelect() throws LanPingException, MaoYanException {
41         computer.run();
42         System.out.println("讲课");
43     }
44 }
45 public class ExceptionTest {
46     public static void main(String[] args) throws LanPingException, MaoYanException {
47         Teacher teacher = new Teacher("毕老师");
48         teacher.prelect();
49     }
50 }

 

自定义异常

原文:http://www.cnblogs.com/sherrykid/p/4604049.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!