您可以在 Java 中创建自己的异常。在编写自己的异常类时,请记住以下几点 -
所有异常必须是 Throwable 的子级。
如果要编写由 Handle 或 Declare Rule 自动强制执行的已检查异常,则需要扩展 Exception 类。
如果要编写运行时异常,您需要扩展 RuntimeException 类。
我们可以定义自己的 Exception 类,如下所示 -
class MyException extends Exception {
}
您只需扩展预定义的 Exception 类来创建您自己的异常。这些被认为是检查异常。下面的 InsufficientFundsException 类是一个用户定义的异常,它扩展了 Exception 类,使其成为受检查的异常。异常类与任何其他类一样,包含有用的字段和方法。
示例 1
class InsufficientFundsException extends Exception {
private double amount;
public InsufficientFundsException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
为了演示如何使用我们的用户定义异常,以下 CheckingAccount 类包含withdraw()抛出 InsufficientFundsException 的方法。
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 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;
}
}
下面的 BankDemo 程序演示了调用 CheckingAccount 的 Deposit() 和 Withdraw() 方法。
package com.yxjc123;
public class BankDemo {
public static void main(String [] args) {
CheckingAccount c = 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 (InsufficientFundsException e) {
System.out.println("Sorry, but you are short $" + e.getAmount());
e.printStackTrace();
}
}
}
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 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;
}
}
class InsufficientFundsException extends Exception {
private double amount;
public InsufficientFundsException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
输出
编译全部以上三个文件并运行BankDemo。这将产生以下结果 -
Depositing $500...
Withdrawing $100...
Withdrawing $600...
Sorry, but you are short $200.0
com.yxjc123.InsufficientFundsException
at com.yxjc123.CheckingAccount.withdraw(BankDemo.java:39)
at com.yxjc123.BankDemo.main(BankDemo.java:14)
在下一个示例中,我们将自定义异常声明为 RuntimeException,使其成为未经检查的异常类,如下 -
class MyException extends RuntimeException {
}
示例 2
我们正在扩展预定义的 RuntimeException 类来创建您自己的异常作为未经检查的异常。下面的 InsufficientFundsException 类是一个用户定义的异常,它扩展了 RuntimeException 类,使其成为未经检查的异常。 RuntimeException 类与任何其他类一样,包含有用的字段和方法。
class InsufficientFundsException extends RuntimeException {
private double amount;
public InsufficientFundsException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
以下 BankDemo 程序演示了如何使用未经检查的异常调用 CheckingAccount 的 Deposit() 和 Pulling() 方法。
package com.yxjc123;
public class BankDemo {
public static void main(String [] args) {
CheckingAccount c = new CheckingAccount(101);
System.out.println("Depositing $500...");
c.deposit(500.00);
System.out.println("\nWithdrawing $100...");
c.withdraw(100.00);
System.out.println("\nWithdrawing $600...");
c.withdraw(600.00);
}
}
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) {
if(amount <= balance) {
balance -= amount;
}else {
double needs = amount - balance;
throw new InsufficientFundsException(needs);
}
}
public double getBalance() {
return balance;
}
public int getNumber() {
return number;
}
}
class InsufficientFundsException extends RuntimeException {
private double amount;
public InsufficientFundsException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
输出
编译并运行BankDemo。这将产生以下结果 -
Depositing $500...
Withdrawing $100...
Withdrawing $600...
Exception in thread "main"
com.yxjc123.InsufficientFundsException
at com.yxjc123.CheckingAccount.withdraw(BankDemo.java:35)
at com.yxjc123.BankDemo.main(BankDemo.java:13)