java Excption example
来源:互联网 发布:淘宝采集软件贵吗 编辑:程序博客网 时间:2024/06/05 03:16
public class ThrowException {
class InsufficientException extends Exception {
private double amount;
public InsufficientException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
class CheckingAccount {
private double balance;
private int number;
public CheckingAccount(int number) {
this.number = number;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) throws InsufficientException {
if(amount <= balance) {
balance -= amount;
}else {
double needs = amount - balance;
throw new InsufficientException(needs);
}
}
public double getBalance() {
return balance;
}
public int getNumber() {
return number;
}
}
public static void main(String [] args) {
ThrowException throwE = new ThrowException();
ThrowException.CheckingAccount c = throwE.new CheckingAccount(101);
System.out.println("Depositing $500...");
c.deposit(500.00);
try {
System.out.println("\nWithdrawing $100...");
c.withdraw(100.00);
System.out.println("\nWithdrawing $600...");
c.withdraw(600.00);
}catch(InsufficientException e) {
System.out.println("Sorry, but you are short $" + e.getAmount());
e.printStackTrace();
}
}
}
class InsufficientException extends Exception {
private double amount;
public InsufficientException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
class CheckingAccount {
private double balance;
private int number;
public CheckingAccount(int number) {
this.number = number;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) throws InsufficientException {
if(amount <= balance) {
balance -= amount;
}else {
double needs = amount - balance;
throw new InsufficientException(needs);
}
}
public double getBalance() {
return balance;
}
public int getNumber() {
return number;
}
}
public static void main(String [] args) {
ThrowException throwE = new ThrowException();
ThrowException.CheckingAccount c = throwE.new CheckingAccount(101);
System.out.println("Depositing $500...");
c.deposit(500.00);
try {
System.out.println("\nWithdrawing $100...");
c.withdraw(100.00);
System.out.println("\nWithdrawing $600...");
c.withdraw(600.00);
}catch(InsufficientException e) {
System.out.println("Sorry, but you are short $" + e.getAmount());
e.printStackTrace();
}
}
}
0 0
- java Excption example
- excption
- 一大堆excption
- JAVA EXAMPLE
- Python[十三]:Excption
- [JAVA]example for jfreechart
- java code example site
- java UDP example
- Java: Simple HTTPUrlConnection example
- java反射机制example
- JSONObject example in Java
- Java MD5 Hashing Example
- java Reflection example
- Java Queue Example
- Java Set Example
- Java Load Properties example
- Java String Split Example
- (Core-JAVA) Deadlock Example
- 项目中用到的一个小插件,基于jQuery的消息提醒 toastr.js
- POJ 1174 Contact 笔记
- windows 7系统安装与配置Tomcat服务器环境
- TCP-IP详解卷-基础知识
- 浅析Windows消息循环
- java Excption example
- Address already in use: JVM_Bind(端口冲突)
- 去除字符串 两端的空格 功能
- 中兴OLTC220单播的配置
- 一个关于jQuery的网站供大家学习
- 二、java网络io编程(BIO、NIO)
- Recall, Precision, and Average Precision
- Android自适应大小TextView
- Centos挂载硬盘