Throwing exceptions from unimplemented classes?

Doctor Bill docbill@gmail.com
Tue Oct 12 17:46:00 GMT 2004


Excuse the newbie question, but has there been any consideration given
to throwing a RuntimeException from unimplemented classes? This would
greatly aid those who want to make their code compatible with gcj,
without putting needless additional checks.
For example, I have a method I have an applet that uses Java
reflection to load the a Swing class at runtime and if the class is
not available it will then default to using the next best AWT class.
public Container createSplitPlane(Component leftPane,Component centerPane)
{
 Container retval = null;
 try
 {
 final Class jsplitClass =
 Class.forName("javax.swing.JSplitPane");
 final Class[] params =
 {Integer.TYPE, Component.class, Component.class};
 final java.lang.reflect.Constructor jsplitConstructor =
 jsplitClass.getConstructor(params);
 final java.lang.reflect.Field horizontalField =
 jsplitClass.getField("HORIZONTAL_SPLIT");
 final Object horizontal =
 horizontalField.get(jsplitClass);
 final Object[] args =
 {horizontal, leftPane, centerPane};
 retval = (Container)jsplitConstructor.newInstance(args);
 }
 catch(final Throwable ignored)
 {
 retval = new Panel(new BorderLayout());
 retval.add(leftPane, BorderLayout.WEST);
 retval.add(centerPane, BorderLayout.CENTER);
 }
 return retval;
 
This type of code works fine so long as either the Swing class is not
present, or if it is present and work. However, if the Swing class is
a dummy implementation, then the code will use the dummy
implementation, instead of defaulting to an AWT class that works.
However, it if the dummy JSplitPane class threw a RuntimeException,
then the code would correctly default to the working class version. 
If the code is in development, and may or may not work, the a
System.property could be set to indicate whether or not a Runtime
exception should be thrown.
 Bill


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /