Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-24-2007, 10:33 PM   PM User | #1
otnj2ee
Regular Coder

 
Join Date: Apr 2007
Posts: 174
Thanks: 17
Thanked 0 Times in 0 Posts
otnj2ee is an unknown quantity at this point
Try to understand Throwable

I saw some sample codes like:

[Base Exception Class]
public class BaseException extends Exception {

public BaseException() {super();}


public BaseException(Throwable exception) {
super();
this.nestedException = exception;
}//end constructor

}

[Sub Exception]
public class TestException extends BaseException {

public TestException(Throwable exception) {
super(exception);
}

public TestException() {
super();
}

}


[Calling Class]
public Result getData() throws TestException {

try {
connection = ConnectionHelper.getConnection(...);

} catch (SQLException e){

TestException te = new TestException(e);
te.setErrorCode("10000");
throw te;
}


--What is the purpose of passing the e as Throwable in

new TestException(e);


It seems to me that if use:

TestException te = new TestException();

will also create the object. And
te.setErrorCode("10000");
throw te;

will be just fine. Then why should use the Throwable e to initiate the Object
TestException? Or what is the usage of use the Throwable?

Thanks


Scott

Last edited by otnj2ee; 05-24-2007 at 10:37 PM..
otnj2ee is offline   Reply With Quote
Old 05-25-2007, 11:41 AM   PM User | #2
ess
Regular Coder

 
Join Date: Oct 2006
Location: United Kingdom
Posts: 865
Thanks: 7
Thanked 29 Times in 28 Posts
ess will become famous soon enough
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
ess is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:49 AM.


Advertisement
Log in to turn off these ads.