I am using Com4J to interact with Microsoft Outlook. I have generated the Java type definitions as per the Com4J tutorial. Here is an example of some code that waits for the user to close an email.
// Registers my event handler
mailItem.advise(
ItemEvents.class,
new ItemEvents() {
@Override
public void close(Holder<Boolean> cancel) {
// TODO Auto-generated method stub
super.close(cancel);
System.out.println("Closed");
}
}
);
// Displays the email to the user
mailItem.display();
This code successfully displays the email to the user. Unfortunately, my program never prints "Closed" when the user closes the window.
When Com4J generates an event class (
ItemEventsin my scenario), the default behavior for all generated methods is to throw anUnsupportedOperationException(see thecom4j.tlbimp.EventInterfaceGeneratorclass for details).For example, here is the
closemethod of theItemEventsclass that my anonymous class overrides:Therefore, when my anonymous class calls
super.close(cancel);, the parent class throws anUnsupportedOperationException, preventing execution from reaching mySystem.out.println("Closed");statement. Therefore, my anonymous class should really have looked like this:What surprised me is that Com4J appears to have simply ignored the
UnsupportedOperationExceptionthrown from the event handler altogether, leaving me no indication of what actually happened. I wrote this code to demonstrate:The program emits this output:
However, there is no indication that a
RuntimeExceptionwas ever thrown.