How do I set the expected exception type for a catch statement with a parameter...
source link: https://www.codesd.com/item/how-do-i-set-the-expected-exception-type-for-a-catch-statement-with-a-parameter-i-passed-to-a-method.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How do I set the expected exception type for a catch statement with a parameter I passed to a method?
I'm trying to define a method like the following:
static def myMethod(expectedExceptionType, Closure closure) {
try {
closure()
} catch (expectedExceptionType e) {
println "Threw an exception: ${e.getMessage()}"
}
}
and run it with something like this:
MyClass.myMethod(NullPointerException) {
doSomeStuff()
}
I basically want to be able to pass in a type to my method and then use that actual parameter to define the formal parameter's type in the catch block of my method.
Is this possible?
A Java based solution can be found in GroovyAssert#shouldFail. The Groovy version of this would not look all that much different. But I would suggest just to use that class, since it is part of the Groovy distribution anyway
Related Articles
How do I set the default status image for UIButton?
How do I set the attachment file encoding for a System.Net.Mail.MailMessage?
How do I set the default date format for Twig models in Symfony2?
How do I set the default working directory for JUnit Launch Configurations in Eclipse?
how to dynamically set the minPointLength except 0
How do I set the Perl inclusion path for modules for a CGI script?
How can I set the min-width size for my pane in Vaadin?
how to correctly set the Chinese file name for different browsers when downloading the file
How do I set the maximum execution time for a PHP script?
How do I set the percentage in Javascript for my progress bar?
How do I set the order of tabs for multiple controls in XAML
s3cmd to set the correct content type for image / jpeg
How do I set the dynamic link path for a shared library?
How do I know the compatible java type for the ResultSet column type in runtime?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK