异常错误
ArithmeticException——由于除数为0引起的异常;
ArrayStoreException——由于数组存储空间不够引起的异常;
ClassCastException—一当把一个对象归为某个类,但实际上此对象并不是由这个类 创建的,也不是其子类创建的,则会引起异常;
IllegalMonitorStateException——监控器状态出错引起的异常;
NegativeArraySizeException—一数组长度是负数,则产生异常;
NullPointerException—一程序试图访问一个空的数组中的元素或访问空的对象中的 方法或变量时产生异常;
OutofMemoryException——用new语句创建对象时,如系统无法为其分配内存空 间则产生异常;
SecurityException——由于访问了不应访问的指针,使安全性出问题而引起异常;
IndexOutOfBoundsExcention——由于数组下标越界或字符串访问越界引起异常;
IOException——由于文件未找到、未打开或者I/O操作不能进行而引起异常;
ClassNotFoundException——未找到指定名字的类或接口引起异常;
CloneNotSupportedException——一程序中的一个对象引用Object类的clone方法,但 此对象并没有连接Cloneable接口,从而引起异常;
InterruptedException—一当一个线程处于等待状态时,另一个线程中断此线程,从 而引起异常,有关线程的内容,将在下一章讲述;
NoSuchMethodException一所调用的方法未找到,引起异常;
Illega1AccessExcePtion—一试图访问一个非public方法;
StringIndexOutOfBoundsException——访问字符串序号越界,引起异常;
ArrayIdexOutOfBoundsException—一访问数组元素下标越界,引起异常;
NumberFormatException——字符的UTF代码数据格式有错引起异常;
IllegalThreadException—一线程调用某个方法而所处状态不适当,引起异常;
FileNotFoundException——未找到指定文件引起异常;
EOFException——未完成输入操作即遇文件结束引起异常。
public class BankDemo {
public static void main(String args[]){
CheckingAccont c = new CheckingAccont(101);
System.out.println("Depositing ¥500..");
c.deposit(500.00);
System.out.println("\nWithdrawing $100");
try {
c.withdraw(100.00);
} catch (InsufficientFundsException e) {
e.printStackTrace();
}
System.out.println("\nno");
try {
c.withdraw(600.00);
} catch (InsufficientFundsException e) {
e.printStackTrace();
}
}
}
public class CheckingAccont {
private double balance;
private int number;
public CheckingAccont(int number){
this.number = number;
}
//方法:存钱
public void deposit(double amount){
balance += amount;
}
//方法:取钱
public void withdraw(double amount)throws InsufficientFundsException{
if(amount<= balance){
balance -= amount;
}
else {
double needs = amount - balance;
throw new InsufficientFundsException(needs);
}
}
public double getBalance(){
return balance;
}
public int getNumber(){
return number;
}
}
import java.io.*;
public class InsufficientFundsException extends Exception{
private double amount;
public InsufficientFundsException(double amount){
this.amount = amount;
}
public double getAmount() {
return amount;
}
}