package ltb6w;
import java.util.*; public class Bank { private String select;
private String select2; private double balance=0.0; //余额
private double inputbalance=0.0; private Scanner sc=new Scanner(System.in); public Bank() { System.out.println("请输入存款额度:"); } @SuppressWarnings("serial")
class NotSufficientFunds extends Exception { //自定义异常类 private String insufficient; NotSufficientFunds(String s ) { insufficient=s;
} public String getMessage() { return insufficient; } } public void deposite() throws Exception{//存款操作 inputbalance=sc.nextInt(); if(inputbalance<0) { throw new NotSufficientFunds("存款不能是负数"); //抛出自定义异常
} this.balance=inputbalance+balance; System.out.println("存款总额:"+balance);
} public void withdrawa() throws Exception{//取款操作 System.out.println("请输入取款数:"); this.balance=balance-sc.nextInt(); if (balance<0) { //激发异常类 throw new NotSufficientFunds("余额已经是负数了"); } System.out.println("余额:"+this.getbalawal());
} public double getbalawal() { //获取余额操作 return balance;
} public static void main(String[] args) { Bank b=new Bank(); while (true) { try {
b.deposite();
} catch (Exception e) { e.printStackTrace();
} System.out.println("是否继续存款:是or否"); try {
b.select=b.sc.next(); // 容易出异常的地方 }catch (InputMismatchException e) { System.out.println("不要输入(是or否)之外无关的存款数据,从来,再来。"); continue; } if("否".equals(b.select)) { while (true) { System.out.println("看仔细了!!!是否还继续取款:是or否"); try {
b.select2=b.sc.next(); // 容易出异常的地方 }catch (InputMismatchException e) { System.out.println("不要输入(是or否)之外无关的数据,从来,再来。"); continue; } if("是".equals(b.select2)) { try {
b.withdrawa(); break;
} catch (Exception e) { e.printStackTrace();
} }else if ("否".equals(b.select2)){ System.out.println("不管你选不选否都当做退出。["+b.select2+"]"); break;
} } System.out.println("已经成功退出"); break; }else if("是".equals(b.select)) {
System.out.println("继续输入金额,come on baby!");
} } } }

自定义抛出throw 对象练习的更多相关文章

  1. 【优化】自定义抛出throw 对象练习

    package ltb6w; import java.util.*; public class Bank { private boolean bool=true; private String sel ...

  2. java中异常的抛出:throw throws

    java中异常的抛出:throw throws Java中的异常抛出 语法: public class ExceptionTest{ public void 方法名(参数列表) throws 异常列表 ...

  3. java中抛出throw关键字是怎么用的? 举例?

    5.抛出throw关键字 马克-to-win:我们先说5/0的原理,当程序运行到5/0的时候,java系统JVM会在后台new出一个除0异常实例,之后把这个实例传入catch块儿供开发者使用.马克-t ...

  4. java 異常抛出 throw 與 return

    package 異常;    public class TestException {      public TestException() {      }        boolean test ...

  5. java-异常-原理异常对象的抛出throw

    1 class Demo { 2 public static int method(int[] arr,int index) { 3 4 // System.out.println(arr[index ...

  6. java自定义抛出的异常Exception

    package com.zhanzhuang.exception; public class CustomizeException { public static void main(String[] ...

  7. 133.throw机制 抛出类类型

    #include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...

  8. Java异常处理-----抛出处理

    抛出处理 定义一个功能,进行除法运算例如(div(int x,int y))如果除数为0,进行处理. 功能内部不想处理,或者处理不了.就抛出使用throw new Exception("除数 ...

  9. 微信小程序-自定义方法的抛出与引用

    一. 定义方法与抛出(utils/foo.js文件中) function say () { console.log('自定义的say方法')}  # 定义方法 module.exports = {sa ...

随机推荐

  1. Ubuntu add sudo

    为了安全起见,ubuntu中的普通用户一般没有root权限,因此即使知道管理员密码也无法使用sudo,但这个情况可以通过加入sudoer列表或者加入sudo组来改变. 拓展: 不管使用哪种方式,使得一 ...

  2. C++中输出字符到文本文档

    #include <iostream> #include <fstream> //ofstream类的头文件 using namespace std; int main() { ...

  3. Unity 3D观察者设计模式-C#委托和事件的运用

    C#观察者设计模式 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创新! ...

  4. LOJ3048 「十二省联考 2019」异或粽子

    题意 题目描述 小粽是一个喜欢吃粽子的好孩子.今天她在家里自己做起了粽子. 小粽面前有 $n$ 种互不相同的粽子馅儿,小粽将它们摆放为了一排,并从左至右编号为 $1$ 到 $n$.第 $i$ 种馅儿具 ...

  5. linux ssh的安装与配置以及实现密钥登录

    安装ssh服务器: yum install openssh ssh 配置文件位于/etc/ssh/sshd_config 如果需要允许root用户远程登录,那么vi /etc/ssh/sshd_con ...

  6. SpringJDBC——jdbcTemplate模板

    一.定义 Spring框架对jdbc进行了封装,提供的一个JDBCTemplated对象简化jdbc开发. 使用步骤 1 导包spring-beans-5.0.0-RELEASE.jar,spring ...

  7. Collection接口中方法的使用

    Collection:集合的接口 1.Collection和ArrayList和List的关系 ArrayList      implement(实现)       List List        ...

  8. MySQL Lock--MySQL加锁规则

    ===================================================================== 淘宝林晓斌总结 在可重复读事务隔离级别下,加锁规则如下: 原 ...

  9. python之路---11 第一类对象 函数名 闭包 迭代器

    二十九. 1.函数名的运用    ①函数名是⼀个变量, 但它是⼀个特殊的变量, 与括号配合可以执⾏函数的变量 ②函数名是一个内存地址    ③ 函数名可以赋值给其他变量         ④函数名可以当 ...

  10. 论 微服务 和 Entity Framework 对 数据 的 割裂

    微服务 的 本质 是 面向对象, 微服务 是 面向对象 对 数据中心 发起的挑战, 在 微服务 架构下, “数据为中心” 的 传统架构 被 严重 割裂, 微服务 的 先天矛盾, 是 对象 和 数据 的 ...