Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using a Groovy Trait to make exceptions usable for flow control.
@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