Skip to content

Commit b0bb1a0

Browse files
committed
HV-2059 Add missing tryFinally function
1 parent f00ce9c commit b0bb1a0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Jenkinsfile

+30
Original file line numberDiff line numberDiff line change
@@ -627,3 +627,33 @@ void mvn(String args) {
627627
sh "mvn $args"
628628
}
629629
}
630+
631+
// try-finally construct that properly suppresses exceptions thrown in the finally block.
632+
def tryFinally(Closure main, Closure ... finallies) {
633+
def mainFailure = null
634+
try {
635+
main()
636+
}
637+
catch (Throwable t) {
638+
mainFailure = t
639+
throw t
640+
}
641+
finally {
642+
finallies.each {it ->
643+
try {
644+
it()
645+
}
646+
catch (Throwable t) {
647+
if ( mainFailure ) {
648+
mainFailure.addSuppressed( t )
649+
}
650+
else {
651+
mainFailure = t
652+
}
653+
}
654+
}
655+
}
656+
if ( mainFailure ) { // We may reach here if only the "finally" failed
657+
throw mainFailure
658+
}
659+
}

0 commit comments

Comments
 (0)