Java - 自定义异常
Java 自定义异常
自定义异常指的是创建自己的异常类,以根据需求定制异常。自定义异常派生自 Exception 类。
Java 自定义异常的需求
- 根据项目中不同类型的错误对异常进行分类。
- 允许应用程序级别的异常处理。
在 Java 中创建自定义异常
要创建自定义异常,需要创建一个继承自 Exception 类的类。
语法
以下是在 Java 中创建自定义类的语法 -
class MyException extends Exception {
}
只需扩展预定义的 Exception 类即可创建自己的异常。这些被视为 checked exceptions。
创建自定义异常的规则
编写自己的异常类时,请注意以下几点 −
所有异常都必须是 Throwable 的子类。
如果要编写一个受 Handle or Declare Rule 自动强制执行的 checked exception,则需要扩展 Exception 类。
如果要编写 runtime exception,则需要扩展 RuntimeException 类。
Java 自定义异常示例
下面的 InsufficientFundsException 类是一个用户定义的异常,它扩展了 Exception 类,使其成为一个 checked exception。异常类就像其他任何类一样,包含有用的字段和方法。
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.;
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..InsufficientFundsException at com..CheckingAccount.withdraw(BankDemo.java:39) at com..BankDemo.main(BankDemo.java:14)
在下一个示例中,我们将自定义异常声明为 RuntimeException,使其成为 unchecked exception 类,如下所示 −
class MyException extends RuntimeException {
}
通过扩展 RuntimeException 创建自定义类的示例
我们通过扩展预定义的 RuntimeException 类来创建自己的异常,作为一个 unchecked exception。下面的 InsufficientFundsException 类是一个用户定义的异常,它扩展了 RuntimeException 类,使其成为 unchecked exception。RuntimeException 类就像其他任何类一样,包含有用的字段和方法。
class InsufficientFundsException extends RuntimeException {
private double amount;
public InsufficientFundsException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
下面的 BankDemo 程序演示了使用 unchecked exception 调用 CheckingAccount 的 deposit() 和 withdraw() 方法。
package com.;
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..InsufficientFundsException at com..CheckingAccount.withdraw(BankDemo.java:35) at com..BankDemo.main(BankDemo.java:13)