码迷,mamicode.com
首页 > 其他好文 > 详细

自定义异常

时间:2015-06-13 19:55:59      阅读:100      评论: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/4573898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!