Is there anyway to catch NoSuchMethodError errors when the groovy code calls a method which doesn't exist?
The following code causes a NoSuchMethodError error which is not caught but the finally block does execute. This causes problems for some of my error handling and reporting.
Code with bogus dsl
try {
println "in try"
dslDoesNotExist()
} catch (Exception ex) {
println "Caught error ${ex}"
throw ex
} finally {
println "finally block"
}
Result
in try
[Pipeline] echo
finally block // see the finally but not the catch
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.NoSuchMethodError: No such DSL method 'dslDoesNotExist'
If I modify the code about to reference a property/variable that does not exist the catch block is processed
Code with missing property
try {
println "in try"
propertyOrVariableDoesNotExist
} catch (Exception ex) {
println "Caught error ${ex}"
throw ex
} finally {
println "finally block"
}
Result
in try
[Pipeline] echo
Caught error groovy.lang.MissingPropertyException: No such property: propertyOrVariableDoesNotExist for class: WorkflowScript
[Pipeline] echo
finally block
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: propertyOrVariableDoesNotExist for class:
What is going on here
Java vs Groovy Exceptions?
Can CPS groovy catch(Exception ex) catch java.lang. exceptions? No
Code
try {
throw new java.lang.NoSuchMethodError("dsl problem")
} catch (Exception ex) {
println "Caught"
}
Result
// No evidence of the catch
[Pipeline] { (hide)
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.NoSuchMethodError: dsl problem