• 自定义异常


     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 }
  • 相关阅读:
    rabbitmq入门
    php7.2 安装redis扩展
    php安装扩展的几种方法
    yum安装php7.2
    相关报错
    [枚举]P1089 津津的储蓄计划
    [DFS]排列的生成
    [枚举]P1085 不高兴的津津
    [模拟]P1047 校门外的树
    [模拟]P1046 陶陶摘苹果
  • 原文地址:https://www.cnblogs.com/sherrykid/p/4573898.html
Copyright © 2020-2023  润新知