Skip to content

Commit

Permalink
HV-2059 Add missing tryFinally function
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Nov 6, 2024
1 parent f00ce9c commit b0bb1a0
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,33 @@ void mvn(String args) {
sh "mvn $args"
}
}

// try-finally construct that properly suppresses exceptions thrown in the finally block.
def tryFinally(Closure main, Closure ... finallies) {
def mainFailure = null
try {
main()
}
catch (Throwable t) {
mainFailure = t
throw t
}
finally {
finallies.each {it ->
try {
it()
}
catch (Throwable t) {
if ( mainFailure ) {
mainFailure.addSuppressed( t )
}
else {
mainFailure = t
}
}
}
}
if ( mainFailure ) { // We may reach here if only the "finally" failed
throw mainFailure
}
}

0 comments on commit b0bb1a0

Please sign in to comment.