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("可以吃了"); } }