Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    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.

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •