5/18/11

Java: Declaring Your own Exceptions tutorial

You can create your own exceptions in Java. Keep the following points in mind when writing your own exception classes:
  1. All exceptions must be a child of Throwable.
  2. If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.
  3. If you want to write a runtime exception, you need to extend the RuntimeException class.



We can define our own Exception class as below:

 class MyException extends Exception{  
 }


You just need to extend the Exception class to create your own Exception class. These are considered to be checked exceptions. The following InsufficientFundsException class is a user-defined exception that extends the Exception class, making it a checked exception. An exception class is like any other class, containing useful fields and methods.


Example: 

// File Name InsufficientFundsException.java
 import java.io.*;

 public class InsufficientFundsException extends Exception 
 {
   private double amount;
   public InsufficientFundsException(double amount)
   {
      this.amount = amount;
   } 
   public double getAmount()
   {
      return amount;
   }
 }



To demonstrate using our user-defined exception, the following CheckingAccount class contains a withdraw() method that throws an InsufficientFundsException.


// File Name CheckingAccount.java
 import java.io.*;

 public 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;
   }
 }



The following BankDemo program demonstrates invoking the deposit() and withdraw() methods of CheckingAccount.


// File Name BankDemo.java
 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();
      }
    }
 }
 


Compile all the above three files and run BankDemo, this would produce following result:
Depositing $500...

Withdrawing $100...

Withdrawing $600...
Sorry, but you are short $200.0
InsufficientFundsException
        at CheckingAccount.withdraw(CheckingAccount.java:25)
        at BankDemo.main(BankDemo.java:13)

SHARE THIS POST:

Related Posts:

  • Create Linked List using Java The LinkedList class extends AbstractSequentialList and implements the List interface. It provides a linked-list data structure. It has the two constructors, shown here: RFAZHE7YNFV8 Link… Read More
  • Java Modifiers Summary The Modifier class provides static methods and constants to decode class and member access modifiers. The sets of modifiers are represented as integers with distinct bit positions representing different modifiers. … Read More
  • Java Servlet Basics Servlet Basics Servlets are Java programs running on a web server that produce  results viewed remotely on a web server. Servlets has the same purpose that CGI or PHP had in the past.Here in this tutorial we … Read More
  • OOPs Concept: Encapsulation Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. RFAZHE7YNFV8 Encapsulation is the technique of making the fields in a class private and provid… Read More
  • Java : Using JDBC API Tutorial You need to use database drivers and the JDBC API while developing a Java application to retrive or store data in a database. The JDBC API classes and interfaces are available in the java.sql  and the javax.sql  … Read More