The Throwable class is the super class or the PARENT CLASS of all Exceptions in the Java programming language.
In other words, if you plan to catch all types of exceptions in your class (IOException, ServletException, ClassNotFoundException, ....etc)...than Throwable is the one to catch.
However, I would not recommend this sort of implementation...as you should be very specific when catching exceptions...so that you can trace back the error...why the error was thrown etc.
For more information on Throwable...you should have a look
http://java.sun.com/j2se/1.3/docs/ap...Throwable.html
Personally, I have always created exceptions to cover basic business rules. For example, if I was to create a Bank application..and want to be sure that account holders cannot withdraw money if they don't have sufficient funds...I would create class that extends the Exception class..and might call it NotEnoughFoundsException. and might use it like so
Code:
public class NotEnoughFoundsException extends Exception {
private double amount;
public NotEnoughFoundsException( String message ) {
super(message);
this.amount = 0.0;
}
public NotEnoughFoundsException( String message, double amount ) {
super(message);
this.amount = amount;
}
public double getAmount() {
return this.amount;
}
public void setAmount( double amount ) {
this.amount = amount;
}
}
public class Tester {
public static void main( String [] args ) {
Account a = new Account( 100 );
try {
a.withdraw( 110 );
} catch( NotEnoughFoundsException e ) {
e.printStackTrace( System.err );
System.out.println( "Currently you have a total of '" + e.getAmount() + "' in your account");
}
}
}
class Account {
private double amount;
public Account( double amount ) {
this.amount = amount;
}
public void withdraw( double amount ) throws NotEnoughFoundsException {
if( ( this.amount - amount ) < 0 ) {
throw new NotEnoughFoundsException( "Sorry, but you cannot withdraw this much from your account", this.amount );
} else {
this.amount -= amount;
}
}
}
Cheers,
Ess