Exception Handling
The Aims
In this exercise, you will create an exception type for when a blacklisted name is used to create an account, or have the name of an account set to the blacklisted name.
Specifying your Account class uses Exceptions
-
Create a new class called DodgyNameException that extends Exception.
-
Add a default constructor to your exception class that calls the superclass constructor passing the String "Fingers is not allowed an account!".
-
Modify both the custom Account class constructor and the Account class setName method to show that they throw a DodgyNameException.
-
In the body of the constructors and the setName method, check that the name parameter is not "Fingers", and if it is, throw the exception.
Click here to see the sample solution
public class DodgyNameException extends Exception {
public DodgyNameException() {
super("Fingers is not allowed an account!");
}
}
public class Account {
...
public Account(double balance, String name, AccountType accountType) throws DodgyNameException {
if (name.equals("Fingers")) {
throw new DodgyNameException();
}
this.balance = balance;
this.name = name;
this.accountType = accountType;
}
...
public void setName(String name) throws DodgyNameException {
if (name.equals("Fingers")) {
throw new DodgyNameException();
}
this.name = name;
}
...
}
Using the Modified Account Class
Edit the TestAccount2 class:
-
Add the appropriate try / catch blocks.
-
In the catch block, print the exception information using the getMessage() method.
-
Run the application and check that the exception does not get thrown. No output should appear.
-
Change the code so that one of the new accounts is in the name of "Fingers".
-
Run it again and see if the exception gets thrown this time. It should be. Your application will now crash, as the account is not created - to avoid this, set the loop which prints out the accounts to ignore the first one .
Click here to see the sample solution
public class TestAccount2 {
public static void main(String[] args) {
Account[] accounts = new Account[5];
try {
accounts[0] = new Account(1000.0, "Fingers", AccountType.CHECKING);
} catch (DodgyNameException e) {
System.out.println(e.getMessage());
}
try {
accounts[1] = new Account(2000.0, "Bob", AccountType.CHECKING);
} catch (DodgyNameException e) {
System.out.println(e.getMessage());
}
try {
accounts[2] = new Account(1500.0, "Charlie", AccountType.SAVING);
} catch (DodgyNameException e) {
System.out.println(e.getMessage());
}
try {
accounts[3] = new Account(1500.0, "Charlie", AccountType.SAVING);
} catch (DodgyNameException e) {
System.out.println(e.getMessage());
}
try {
accounts[4] = new Account(2500.0, "Eve", AccountType.CREDIT_CARD);
} catch (DodgyNameException e) {
System.out.println(e.getMessage());
}
Account.setInterestRate(0.07);
for (int i = 1; i < accounts.length; i++) {
System.out.println("Account: " + accounts[i]);
accounts[i].addInterest();
System.out.println("Balance after interest: " + accounts[i].getBalance());
System.out.println();
}
...
}
}