How the try and catch blocks work?

For the following section of code from the class QueueInheritanceTest...how the try and catch blocks work?

The Class...

public class QueueInheritance extends List {

public QueueInheritance() { super( "queue" ); }

public void enqueue( Object o )

{ insertAtBack( o ); }

public Object dequeue()

throws EmptyListException { return removeFromFront(); }

public boolean isEmpty() { return super.isEmpty(); }

public void print() { super.print(); }

}

Testing...

public class QueueInheritanceTest {

public static void main( String args[] ){

QueueInheritance objQueue = new QueueInheritance();

// Create objects to store in the queue

Boolean b = Boolean.TRUE;

Character c = new Character( '$' );

Integer i = new Integer( 34567 );

String s = "hello";

// Use the enqueue method

objQueue.enqueue( b );

objQueue.enqueue( c );

objQueue.enqueue( i );

objQueue.enqueue( s );

objQueue.print();

// Use the dequeue method

Object removedObj = null;

try { while ( true ) {

removedObj = objQueue.dequeue();

System.out.println(removedObj.toString()+" dequeued" );

objQueue.print();

}

}

catch ( EmptyListException e ) {

System.err.println( "\n" + e.toString() );

}

}

}

[1380 byte] By [printlnOrder] at [2007-9-30 21:15:12]
# 1

If you want a basic introduction to try/catch blocks, read any introductory text or the tutorials on this site.

Here are some:

[url=http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html]Sun's New To Java Center[/url].

[url=http://www.javaranch.com]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns."

In terms of this particular case, it looks like the code is using an exception being thrown to get out of a loop. IMHO that's bad design -- exceptions should be used for exceptional circumstances, and if you use it to get out of a loop, then you're certain it's going to happen, and that means that it's not exceptional.

When you post code, please wrap it in tags so it's legible.

paulcw at 2007-7-7 2:48:13 > top of Java-index,Desktop,Deploying...
# 2
Whoops. That's [code][/code] tags.
paulcw at 2007-7-7 2:48:13 > top of Java-index,Desktop,Deploying...