Class ExceptionStash

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class ExceptionStash
    extends java.lang.Object
    implements java.lang.AutoCloseable
    The intent of this class is to insulate SWT from exceptions occurring in user's listeners, so that SWT remains stable and does not accidentally crash JVM or enter some very broken state. The supposed use is:
    void someSwtInternalFunction() {
         // Make a stash to collect all exceptions from user listeners
         try (ExceptionStash exceptions = new ExceptionStash ()) {
             // Perform some action that may call a throwing user's listener
             try {
                sendEvent(SWT.SomeEvent);
             } catch (Error | RuntimeException ex) {
                 exceptions.stash (ex);
             }
    
             // Do some important SWT stuff that you would normally do in
             // 'finally' clause. With 'ExceptionStash' you can interleave
             // important things with listeners without making code ugly.
             MakeSureThingsDontBreak();
    
             // Perform another action that may call a throwing user's listener.
             // Done in an independent 'try' block to make sure that all events
             // are sent regardless of exceptions in some of the listeners.
             try {
                askWidgetToSendMoreEvents();
             } catch (Error | RuntimeException ex) {
                 exceptions.stash (ex);
             }
    
             // Exiting from 'try' statement will close ExceptionStash and
             // re-throw collected exception. If there are multiple exceptions,
             // all subsequent ones will be added as 'Throwable.addSuppressed()'.
         }
     } 
    • Constructor Summary

      Constructors 
      Constructor Description
      ExceptionStash()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void stash​(java.lang.Throwable throwable)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ExceptionStash

        public ExceptionStash()
    • Method Detail

      • stash

        public void stash​(java.lang.Throwable throwable)
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable