Using a Groovy Trait to make exceptions usable for flow control.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='org.gperfutils', module='gbench', version='0.4.2-groovy-2.1') | |
import java.lang.String | |
trait NoStackTrace { | |
Throwable fillInStackTrace() { | |
return this | |
} | |
} | |
class ExpensiveException extends RuntimeException { } | |
class CheapException extends RuntimeException implements NoStackTrace {} | |
def r = benchmark { | |
'Cheap Exception' { | |
def e = new CheapException() | |
} | |
'Expensive Exception' { | |
def e = new ExpensiveException() | |
} | |
} | |
r.prettyPrint() | |
/** | |
* Environment | |
=========== | |
* Groovy: 2.3.6 | |
* JVM: Java HotSpot(TM) 64-Bit Server VM (25.0-b70, Oracle Corporation) | |
* JRE: 1.8.0 | |
* Total Memory: 231 MB | |
* Maximum Memory: 3641 MB | |
* OS: Mac OS X (10.9.3, x86_64) | |
Options | |
======= | |
* Warm Up: Auto (- 60 sec) | |
* CPU Time Measurement: On | |
user system cpu real | |
Cheap Exception 36 0 36 37 | |
Expensive Exception 7621 2 7623 7625 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment