小白教程

 找回密码
 立即注册
小白教程 首页 系列教程 Java系列教程 查看内容

Java 异常处理

发布者: 小白教程



声明自定义异常

在 Java 中你可以自定义异常。编写自己的异常类时需要记住下面的几点。

  • 所有异常都必须是 Throwable 的子类。

  • 如果希望写一个检查性异常类,则需要继承 Exception 类。

  • 如果你想写一个运行时异常类,那么需要继承 RuntimeException 类。

可以像下面这样定义自己的异常类:

  1. class MyException extends Exception{
  2. }

只继承 Exception 类来创建的异常类是检查性异常类。

下面的 InsufficientFundsException 类是用户定义的异常类,它继承自 Exception。

一个异常类和其它任何类一样,包含有变量和方法。

实例

  1. // 文件名InsufficientFundsException.java
  2. import java.io.*;
  3. public class InsufficientFundsException extends Exception
  4. {
  5.    private double amount;
  6.    public InsufficientFundsException(double amount)
  7.    {
  8.       this.amount = amount;
  9.    } 
  10.    public double getAmount()
  11.    {
  12.       return amount;
  13.    }
  14. }

为了展示如何使用我们自定义的异常类,

在下面的 CheckingAccount 类中包含一个 withdraw() 方法抛出一个 InsufficientFundsException 异常。

  1. // 文件名称 CheckingAccount.java
  2. import java.io.*;
  3. public class CheckingAccount
  4. {
  5.    private double balance;
  6.    private int number;
  7.    public CheckingAccount(int number)
  8.    {
  9.       this.number = number;
  10.    }
  11.    public void deposit(double amount)
  12.    {
  13.       balance += amount;
  14.    }
  15.    public void withdraw(double amount) throws
  16.                               InsufficientFundsException
  17.    {
  18.       if(amount <= balance)
  19.        {
  20.           balance -= amount;
  21.        }
  22.        else
  23.        {
  24.           double needs = amount - balance;
  25.           throw new InsufficientFundsException(needs);
  26.        }
  27.     }
  28.     public double getBalance()
  29.     {
  30.        return balance;
  31.     }
  32.     public int getNumber()
  33.     {
  34.        return number;
  35.     }
  36.  }

下面的 BankDemo 程序示范了如何调用 CheckingAccount 类的 deposit() 和 withdraw() 方法。

  1. //文件名称 BankDemo.java
  2. public class BankDemo
  3. {
  4.    public static void main(String [] args)
  5.    {
  6.       CheckingAccount c = new CheckingAccount(101);
  7.       System.out.println("Depositing $500...");
  8.       c.deposit(500.00);
  9.       try
  10.       {
  11.          System.out.println("\nWithdrawing $100...");
  12.          c.withdraw(100.00);
  13.          System.out.println("\nWithdrawing $600...");
  14.          c.withdraw(600.00);
  15.       }catch(InsufficientFundsException e)
  16.       {
  17.          System.out.println("Sorry, but you are short $"
  18.                                   + e.getAmount());
  19.          e.printStackTrace();
  20.       }
  21.     }
  22. }

编译上面三个文件,并运行程序 BankDemo,得到结果如下所示:

  1. Depositing $500...
  2. Withdrawing $100...
  3. Withdrawing $600...
  4. Sorry, but you are short $200.0
  5. InsufficientFundsException
  6.         at CheckingAccount.withdraw(CheckingAccount.java:25)
  7.         at BankDemo.main(BankDemo.java:13)


上一篇:Java数据类型转换下一篇:Java 继承

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 08:44 , Processed in 0.024038 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

返回顶部