FileDocCategorySizeDatePackage
SuperfluousExceptions.javaAPI DocExample6043Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.exceptions

SuperfluousExceptions

public class SuperfluousExceptions extends Object
Demonstration of having to catch superfluous exceptions.
author
Robert Simmons jr. (kraythe)
version
$Revision: 1.4 $

Fields Summary
Constructors Summary
Methods Summary
public voidprepareJDBCDriver()
Method to prepare a JDBC Driver.

throws
ClassNotFoundException If the JDBS driver name given isn't found.

		Class.forName("com.mysql.jdbc.Driver");
	
public voidreflectionMethod()
A Sample Reflection method.

throws
NoSuchMethodException If a method cant be located.
throws
IllegalAccessException If the security manager rejects access.
throws
InvocationTargetException If the invoked method throws an exception.
throws
ClassNotFoundException If a class cant be found.
throws
MyCustomException If there is a business logic error.

		Class.forName("oreilly.hcj.bankdata.Gender");
		// .. some code
		Method meth = Gender.class.getDeclaredMethod("getName", null);
		meth.invoke(Gender.MALE, null);
		// .. other code working with result that might 
		// throw a program exception
	
public voidreflectionMethod2()
A Sample Reflection method.

throws
MyCustomException If there is a business logic error.
throws
RuntimeException If there is an reflection-based exception.

		try {
			Class.forName("oreilly.hcj.bankdata.Gender");
			// .. some code
			Method meth = Gender.class.getDeclaredMethod("getName", null);
			meth.invoke(Gender.MALE, null);
			// .. other code working with result that might 
			// throw a program exception
		} catch (final NoSuchMethodException ex) {
			throw new RuntimeException(ex);
		} catch (final IllegalAccessException ex) {
			throw new RuntimeException(ex);
		} catch (final InvocationTargetException ex) {
			throw new RuntimeException(ex);
		} catch (final ClassNotFoundException ex) {
			throw new RuntimeException(ex);
		}
	
public voidreflectionMethod3()
A Sample Reflection method.

throws
MyCustomException If there is a business logic error.
throws
RuntimeException If there is an reflection-based exception.

		try {
			Class.forName("oreilly.hcj.bankdata.Gender");
			// .. some code
			Method meth = Gender.class.getDeclaredMethod("getName", null);
			meth.invoke(Gender.MALE, null);
			// .. other code working with result that might 
			// throw a program exception
		} catch (final Exception ex) {
			if (ex instanceof MyCustomException) {
				throw (MyCustomException)ex;
			}
			throw new RuntimeException(ex);
		}
	
public voidreflectionMethod4()
A Sample Reflection method.

throws
MyCustomException If there is a business logic error.

		try {
			Class.forName("oreilly.hcj.bankdata.Gender");
			// .. some code
			Method meth = Gender.class.getDeclaredMethod("getName", null);
			meth.invoke(Gender.MALE, null);
			// .. other code working with result that might 
			// throw a program exception
		} catch (final Exception ex) {
			if (ex instanceof MyCustomException) {
				throw (MyCustomException)ex;
			}
		}
	
public voidreflectionMethod5(java.lang.String[] packageList)
A Sample Reflection method.

param
packageList The list of packages to find a class in.
throws
MyCustomException If there is a business logic error.
throws
RuntimeException If there is an reflection-based exception.

		try {
			Class.forName("oreilly.hcj.bankdata.Gender");
			// .. some code
			Method meth = Gender.class.getDeclaredMethod("getName", null);
			meth.invoke(Gender.MALE, null);

			// .. other code working with result that might 
			// throw a program exception
			// try to load a class by searching through some packages.
			for (int idx = 0; idx < packageList.length; idx++) {
				try {
					Class.forName(packageList[idx] + "MyClass");
				} catch (final ClassNotFoundException ex) {
					// not an error. 
				}
			}
		} catch (final NoSuchMethodException ex) {
			throw new RuntimeException(ex);
		} catch (final IllegalAccessException ex) {
			throw new RuntimeException(ex);
		} catch (final InvocationTargetException ex) {
			throw new RuntimeException(ex);
		} catch (final ClassNotFoundException ex) {
			throw new RuntimeException(ex);
		}
	
public voidsomeMethod()
Demonstrates hazards fo using Exception in a throws clause.

throws
Exception If there is a problem.
throws
MyCustomException If there is a business logic problem.

		try {
			// .. some code that starts a purchase transaction..
			reflectionMethod();
		} catch (final Exception ex) {
			if (ex instanceof MyCustomException) {
				if (((MyCustomException)ex).getType() == MyCustomException.CREDIT_CARD_DECLINED) {
					// check if customer has other cards, 
					// if not throw a billing error. 
					throw new MyCustomException(MyCustomException.BILLING_ERROR);
				}
			} else {
				throw ex;
			}
		}