java - 使用 def 异常无法转换为可抛出错误

标签 java exception

<分区>

public class Odexception extends Exception {
     double deficit;
     Odexception(String msg,double def) {
         super(msg);
         this.deficit=def;
     }
}


public   void withdraw(double amt)  throws odexception {
     if(amt<=balance) {
         balance=balance-amt;
     } else {
          throw new Odexception("Insufficient balance",amt-balance);
     }
}

public static void main(string ar[]) {
     try{
        c[0].acc[0].withdraw(2000);
     } catch(Odexception  e) {
        System.out.println(e.getMessage());
     }
}

最佳答案

这就是你想要的吧?

class Odexception extends Exception{
    double deficit;
    Odexception(String msg,double def) {
        super(msg);
        this.deficit=def;
    }
}

public class CustomException {
    double balance;
    public CustomException(double balance)
    {
        this.balance = balance;
    }

    public void withdraw(double amt) throws Odexception {
        if (amt <= balance) {
            {
                balance = balance - amt;
                System.out.println("Withdrawn : "+amt+"\nCurrent Balance is :"+balance);
            }
        } else {
            throw new Odexception("Insufficient balance", amt - balance);
        }
    }

    public static void main(String ar[]) {
        try {
            new CustomException(1000).withdraw(5000);
        } catch (Odexception e) {
            System.out.println(e.getMessage());
        }
    }
}

关于java - 使用 def 异常无法转换为可抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24323567/

相关文章:

javascript - 清理对象在构造函数中抛出错误

java - 数据库审计表

java - 在 Java/C++ 中创建类单例

java - 如何在java中设置concurrentHashMap的并发级别

java - 捕获异常后代码不工作

java - 类 I/O 的异常(exception)

java - spring 3.12用的是cglib 2.2.2,spark 8.0用的是cglib 3.0,但是需要同时执行?

java - 将 PrintQueue 对象从一台计算机发送到另一台计算机?

python - 重新引起困惑

java - 进度条属性变化事件不触发