java 异常2

自定义异常类:

import java.util.Scanner;

/**
 * Created by lenovo on 2016/8/12.
 */
class NoMoneyException extends Exception {

     NoMoneyException(String s){
        super(s);
    }
}

public class Demo {
    public static void main(String[] args) {
       try{
           eat(8);
       }catch (NoMoneyException e){
           e.toString();
           e.printStackTrace();
           System.out.println("钱不够");
       }
    }

    public static void eat(int money) throws NoMoneyException{//异常的声明
        //int money = 0;
        if(money < 10) {
            throw new NoMoneyException("钱不够");
        }
        System.out.println("可以吃了");
    }
}

 

posted @ 2016-08-13 22:53  lifehrx  阅读(96)  评论(0编辑  收藏  举报