RE: Exceptions (was: RE: [Avalon4:PROPOSAL] Context Consensus)
"Berin Loritsch" <[email protected]>
| Newsgroups | gmane.comp.jakarta.avalon.apps.devel,gmane.comp.jakarta.avalon.devel |
|---|---|
| Message-ID | <[email protected]> |
> >>I agree that container (and all platform code) should be
> >>robust. But here's a rub. There is no guarantee that the
> >>Component won't take the Context, squirrel it away, and cast the
> >>thing later when it needs it. And when THAT happens, who sees the
> >>ClastCastException? Quite likely some other component, whom is not
> >>expecting those kinds of internal errors to leak out.
> >>
> >>
>
> Disagree - the container can handle these conditions - if its not in
> contextualize state then it will be in the initialization
> stage or the
> starup stage, In each case, the container has to take responsibility
> for tracking runtime errors and handling the decommission
> responsibilities.
Not necessarily. Consider this poorly written example:
public interface DoSomethingService {
void doSomething();
}
public class MyBadComponent implements Contextualizable, DoSomethingService
private Context m_context;
/**
* The context is not immediately used or cast--making container
* detection of a bad component during initialization impossible.
*/
public void contextualize( Context context ) {
m_context = context;
}
/**
* The cast is done at runtime, when another component or client uses
* the service. The client code will be the unhappy recipient of the
* ClassCastException.
*/
public void doSomething() {
( (BlockContext) m_context ).requestShutdown();
}
}
The only way to handle this scenario is to have a Proxy surround calls to
the runtime interface and intercept the RuntimeExceptions that should never
be thrown--i.e.. NullPointerException, Class**Exception (NotFound, Cast,
etc.),
and let through RuntimeExceptions that should be thrown--i.e..
IllegalArgumentException.
It also demonstrates that an ambiguous service can do something you don't
really want ;).