HaniiPuppy

Exception handling

Mar 28th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. public <T extends EventArgs> T getUsableCopy()
  2.     {
  3.         // Here we go. A one line method with 18 lines of exception handling.
  4.         // Somewhere, a kitten is dead. God's an evil bastard.
  5.  
  6.         try
  7.         { return (T)this.<T>getClass().getConstructor(this.<T>getClass()).newInstance(this); }
  8.         catch(IllegalAccessException e)
  9.         { throw new RuntimeException("getCopy run on class: " + this.getClass().getSimpleName() + " did not have permission to create a new instance. This is one of the signs of the Apocalypse.", e); }
  10.         catch(InvocationTargetException e)
  11.         {
  12.             Throwable cause = e.getCause();
  13.  
  14.             if(cause instanceof RuntimeException)
  15.                 throw (RuntimeException)cause;
  16.             else if(cause instanceof Error)
  17.                 throw (Error)cause;
  18.             else
  19.                 throw new RuntimeInvocationTargetException("", e.getCause());
  20.         }
  21.         catch(NoSuchMethodException e)
  22.         { throw new RuntimeException("Class: " + this.getClass().getSimpleName() + " doesn't implement a constructor using its own type as a constructor. All descendents of EventArgs should.", e); }
  23.         catch(InstantiationException e)
  24.         { throw new RuntimeException("Unable to instantiate class:" + this.getClass().getSimpleName(), e); }
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment