Skip to content

Instantly share code, notes, and snippets.

@evacchi
Created January 2, 2014 21:09
Show Gist options
  • Save evacchi/8226980 to your computer and use it in GitHub Desktop.
Save evacchi/8226980 to your computer and use it in GitHub Desktop.
Blue.Pill -- RuntimeException with NoStackTrace for Java (inspired by Scala's NoStackTrace trait)
/**
* You take the Blue.Pill. You wake up in your bed and believe whatever you want to.
* Usage: <code>throw Blue.Pill</code>, then, somewhere else:
* try { ... } catch (Blue Pill) { /* possibly do something */ }
*
* @author edoardovacchi
*/
public abstract class Blue extends RuntimeException {
public static Blue.Pill Pill = new Blue.Pill();
/**
* inspired by Scala's NoStackTrace trait
*
* @return this instance, with no stack trace
*/
@Override
public Throwable fillInStackTrace() {
return this;
}
private static class Pill extends Blue {}
}
@evacchi
Copy link
Author

evacchi commented Mar 10, 2014

  void myMethod() { throw Blue.Pill; }
  void foo() {
    try { myMethod(); } catch(Blue Pill) { /* handle here */}
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment