zoukankan      html  css  js  c++  java
  • java 异常练习题1

    建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),表示存入银行100元,当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。

    package exception;
    
    public class Bank {
        double balance;
        public Bank(double balance)
        {
            
        }
        public void withDrawal(double dAmount)  throws Exception
        {
            if(dAmount>balance)
            {
                
                throw new InsufficientFundsException();
                
                
            }
            if(dAmount<0)
            {
                throw new NagativeFundsException();
            }
            
            balance =balance-dAmount;
            
            
        }
    
    }
    package exception;
    
    public class InsufficientFundsException extends Exception {
    
        @Override
        public String getMessage() {
            // TODO 自动生成的方法存根
            return "余额不足!";
        }
        
    
    }
    package exception;
    
    public class NagativeFundsException extends Exception {
    
        @Override
        public String getMessage() {
            // TODO 自动生成的方法存根
            return "取款数不能为负数";
        }
        
    
    }
    package exception;
    
    public class Test1 {
    
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
    
            Bank b=new Bank(100);
            try{ 
                b.withDrawal(150);
            }
            catch (Exception e)
            {
                System.out.println(e.getMessage());
            }
            try{
                b.withDrawal(-15);
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage()) ;
            }
        }
    
    }

  • 相关阅读:
    SpringMVC+Shiro权限管理(转载)
    面试常见问题(转载)
    JavaScript 五种(非构造方式)继承
    JavaScript 五种(构造方式)继承
    Quartz.Net 基于XML配置启动
    jexus防止产生 *.core文件
    Last-Modified、ETag、Expires和Cache-Control
    正则表达式记录
    C# 操作mongodb子文档
    ASP.NET Core "完整发布,自带运行时" 到jexus
  • 原文地址:https://www.cnblogs.com/jskbk/p/5538121.html
Copyright © 2011-2022 走看看