java如何自定义异常转载

原创
小哥 3年前 (2022-10-19) 阅读数 196 #JAVA
文章标签 java异常
package mistake;

class NomoneyException extends Exception{
    public NomoneyException(String message){
        super(message);//调用Exception的单参数构造函数的单参数构造函数
    }
}
public class selfmistake {

public static void main(String[] args) {
    int money=9;
    try {
        chifan(money);
    } catch (NomoneyException e) {
        e.printStackTrace();
        System.out.println("回去把钱带回来!回去把钱带回来!");
    }
}        
public static void chifan(int money) throws NomoneyException{
    if(money<10){
        throw new NomoneyException("没有钱就别吃东西");
    }else{
        System.out.println("吃地沟油桶米饭!吃地沟油木桶米饭!");
    }
}

}

转载于:https://www.cnblogs.com/LYY1084702511/p/10908478.html

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门