自定义异常

 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 }
原文地址:https://www.cnblogs.com/sherrykid/p/4573898.html