Methods Summary |
---|
public void | prepareJDBCDriver()Method to prepare a JDBC Driver.
Class.forName("com.mysql.jdbc.Driver");
|
public void | reflectionMethod()A Sample Reflection method.
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 void | reflectionMethod2()A Sample Reflection method.
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 void | reflectionMethod3()A Sample Reflection method.
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 void | reflectionMethod4()A Sample Reflection method.
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 void | reflectionMethod5(java.lang.String[] packageList)A Sample Reflection method.
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 void | someMethod()Demonstrates hazards fo using Exception in a throws clause.
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;
}
}
|