FileDocCategorySizeDatePackage
ExceptionDemo.javaAPI DocExample1007Sat Nov 25 12:54:14 GMT 2000None

ExceptionDemo

public class ExceptionDemo extends Object
Simple demo of exceptions

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.
			}
			System.out.println(o);	// process the created object in some way
		}
	
public static voidmain(java.lang.String[] argv)

		new ExceptionDemo().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();