自定义抛出throw 对象练习
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 对象练习的更多相关文章
- 【优化】自定义抛出throw 对象练习
package ltb6w; import java.util.*; public class Bank { private boolean bool=true; private String sel ...
- java中异常的抛出:throw throws
java中异常的抛出:throw throws Java中的异常抛出 语法: public class ExceptionTest{ public void 方法名(参数列表) throws 异常列表 ...
- java中抛出throw关键字是怎么用的? 举例?
5.抛出throw关键字 马克-to-win:我们先说5/0的原理,当程序运行到5/0的时候,java系统JVM会在后台new出一个除0异常实例,之后把这个实例传入catch块儿供开发者使用.马克-t ...
- java 異常抛出 throw 與 return
package 異常; public class TestException { public TestException() { } boolean test ...
- java-异常-原理异常对象的抛出throw
1 class Demo { 2 public static int method(int[] arr,int index) { 3 4 // System.out.println(arr[index ...
- java自定义抛出的异常Exception
package com.zhanzhuang.exception; public class CustomizeException { public static void main(String[] ...
- 133.throw机制 抛出类类型
#include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...
- Java异常处理-----抛出处理
抛出处理 定义一个功能,进行除法运算例如(div(int x,int y))如果除数为0,进行处理. 功能内部不想处理,或者处理不了.就抛出使用throw new Exception("除数 ...
- 微信小程序-自定义方法的抛出与引用
一. 定义方法与抛出(utils/foo.js文件中) function say () { console.log('自定义的say方法')} # 定义方法 module.exports = {sa ...
随机推荐
- Python网络爬虫之requests模块(2)
session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 有些时候,我们在使用爬 ...
- mxnet 动手学深度学习
http://zh.gluon.ai/chapter_crashcourse/introduction.html 强化学习(Reinforcement Learning) 如果你真的有兴趣用机器学习开 ...
- 九度OJ-1042-最长公共子序列(LCS)
题目1042:Coincidence 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4045 解决:2208 题目描述: Find a longest common subsequence ...
- Django中Ajax处理
1.大部分和Flask中相同. 2.Django处理JSON,主要是对于查询结果集处理. 使用Django提供的序列化的类来完成QuerySet到JSON字符串的转换 from django.core ...
- xdoj-1243 (费马平方和问题)
1243: CKJ老师爱数学 时间限制: 1 Sec 内存限制: 128 MB提交: 56 解决: 13[提交][状态][讨论版] 题目描述 众所周知,CKJ老师非常热爱数学,他对于方程组的有自己 ...
- lecture2-word2vec-七月在线nlp
离散表示: one-hot bag of words -- 词权重 ~不能表示顺序关系 TF-IDF (Term Frequency - Inverse Document Frequency) [ ...
- 牛客国庆集训派对Day4 I-连通块计数(思维,组合数学)
链接:https://www.nowcoder.com/acm/contest/204/I 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- 2017.7.21 python statvfs方法读取磁盘容量
实地代码 [maintenance@localhost ~]$ python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150 ...
- eclipse打jar包解决第三方依赖包
在项目根目录下手动MANIFEST.MF(eclipse无法自动生成) MANIFEST.MF Manifest-Version: 1.0 Class-Path: lib/kafka-clients- ...
- python实现单链表的翻转
#!/usr/bin/env python #coding = utf-8 class Node: def __init__(self,data=None,next = None): ...