FileDocCategorySizeDatePackage
ExceptionDemo2.javaAPI DocExample1119Sat Nov 25 12:54:14 GMT 2000None

ExceptionDemo2

public class ExceptionDemo2 extends Object
Simple demo of exceptions, with finally clause

Fields Summary
Constructors Summary
Methods Summary
public voiddoTheWork()
This method demonstrates calling a method that might throw an exception, and catching the resulting exception.

		Object o = null;

		for (int i=0; i<5; i++) {
			try {
				o = makeObj(i);
			} catch (IllegalArgumentException e) {
				System.err.println("Error: (" + e.getMessage() + ").");
				return;		// cut off println below if makeObj failed.
			} finally {
				System.err.println("All done");
				if (i==null)
					System.exit(0);
			}
			System.out.println(o);	// process the created object in some way
		}
	
public static voidmain(java.lang.String[] argv)

		new ExceptionDemo2().doTheWork();
	
public java.lang.ObjectmakeObj(int type)
Model of a method that creates and returns an object. This method is really here to show how you throw exceptions.

exception
IllegalArgumentException if called with value 1.

		if (type == 1)	// detects an error...
			throw new IllegalArgumentException("Don't like type " + type);
		return new Object();