/*
* REQUIRES: accountName has a non-zero length, initialBalance >= 0
* //...
*/
public Account(String accountName, double initialBalance) {
}
AND
throws IllegalNameException, IllegalBalanceException {
if (name == null || name.isEmpty()) {
throw new IllegalNameException();
}
if (initialBalance < 0) {
throw new IllegalBalanceException();
}
// ...
private void init() {
try {
cheq = new Account("Joe", 145.00);
sav = new Account("Joe", 256.50);
} catch (IllegalBalanceException e) {
System.err.println("Illegal balance given to one of the accounts");
} catch (IllegalNameException e) {
System.err.println("Illegal name given to one of the accounts");
}
}
What should we put within one try block?
private void init() {
try {
cheq = new Account("Joe", 145.00);
} catch (IllegalBalanceException e) {
System.err.println("An illegal balance was given to CHECKING account");
} catch (IllegalNameException e) {
System.err.println("An illegal name was given to CHECKING account");
}
try {
sav = new Account("Joe", 256.50);
} catch (IllegalBalanceException e) {
System.err.println("An illegal balance was given to SAVINGS account");
} catch (IllegalNameException e) {
System.err.println("An illegal name was given to SAVINGS account");
}
}
It depends. Sometimes we want to differentiate.
Catching both exceptions!
private void init() {
try {
cheq = new Account("Joe", 145.00);
sav = new Account("Joe", 256.50);
} catch (Exception e) {
System.err.println("Something went wrong...");
}
}
What if we don't care about the type of exception?
Both our exceptions extend the Exception class!
public class AccountCreationException
extends Exception {}
public class IllegalBalanceException
extends AccountCreationException {}
public class IllegalNameException
extends AccountCreationException {}
AccountCreationException e
Throwable
Object
Error
Exception
RuntimeException
...
...
Unchecked Exceptions
Checked Exceptions
private void init() throws CouldNotStartException {
try {
cheq = new Account("Joe", 145.00);
sav = new Account("Joe", 256.50);
} catch (AccountCreationException e) {
System.err.println("Something went wrong during account creation");
throw new CouldNotStartException();
}
}
public static void main(String[] args) {
try {
new TellerApp();
} catch (CouldNotStartException e) {
System.err.println("Could not start the app");
}
}
public class CouldNotStartException
extends RuntimeException {}
Note: unchecked exceptions in many cases indicate bad programming. We often don't want to explicitly throw/catch these!
private void init() throws CouldNotStartException {
try {
cheq = new Account("Joe", 145.00);
sav = new Account("Joe", 256.50);
} catch (AccountCreationException e) {
System.err.println("Something went wrong during account creation");
throw new CouldNotStartException();
}
}
public static void main(String[] args) {
try {
new TellerApp();
} catch (CouldNotStartException e) {
System.err.println("Could not create accounts exception");
}
}
Catching is optional!
-ea
balance >= 0
assert(balance >= 0);