7

How do I set the expected exception type for a catch statement with a parameter...

 3 years ago
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?

advertisements

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?

i am totally confused. I just want to set an Image for the Default-State of a list of UIButtons. But in does not work. I simply assigned the image to all my Buttons inside the setter of my Outlet-Collection but the Image just appears if the Button is

How do I set the attachment file encoding for a System.Net.Mail.MailMessage?

I use System.Net.Mail to send out a series of MailMessage with Attachment. To do this, I fill a MemoryStream with data, using a StreamWriter with Encoding.UTF8. I then create an Attachment using the MemoryStream. I can set the MIME type for the attac

How do I set the default date format for Twig models in Symfony2?

Twig documentation describes how to set the default date format for the date filter: $twig = new Twig_Environment($loader); $twig->getExtension('core')->setDateFormat('d/m/Y', '%d days'); How can do this setting globally in Symfony2?For a more detai

How do I set the default working directory for JUnit Launch Configurations in Eclipse?

Our Java code (not the test code) reads files from the current directory, which means the working directory needs to be set properly whenever we run the code. When launching a JUnit test from within Eclipse, a launch configuration automatically gets

how to dynamically set the minPointLength except 0

I like to set dynamically the value of the minPointLengh for high chart. But the below mentioned code is not working. can you please help me to get it done. $(function () { $('#container').highcharts({ chart: { type: 'column' }, xAxis: { categories:

How do I set the Perl inclusion path for modules for a CGI script?

I've got several Perl modules installed on my hosting machine in ~/perl, how do I add them to Perl module path? Setting PERL5LIB or unshifting the paths to @INC surely works, but the environment variable does not help when running as a CGI script and

How can I set the min-width size for my pane in Vaadin?

In my Vaadin code I want to add min-width feature to my sub-window. I set width of my subwindow size %25 of my mainwindow, but I don't want it to be very small.I want it at least "125px". How can i set this with java code? public class Awindow e

how to correctly set the Chinese file name for different browsers when downloading the file

I set like this: response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("你好.txt", "utf-8")); it works in chrome, but not in firefox, in firefox the file name is %E4%BD%A0%E5%A5%BD.txt.

How do I set the maximum execution time for a PHP script?

I would like to change the maximum execution time for a PHP script. In the script I have tried ini_set("max_execution_time", "1000"); and set_time_limit(1000); together and separately. I also added this line to .htaccess: php_value max

How do I set the percentage in Javascript for my progress bar?

I do not know anything in Javascript (I have copied a code for a progress bar but it does not display the percentage). I just need to display the text value of the actual % inside my progress bar (a text such as : 1%, 2%, 3%...). The existing code I

How do I set the order of tabs for multiple controls in XAML

Do I have to manually set the TabIndex property of each control on my page, or is there a way I can select multiple controls and get either Expression Blend or Visual Studio to apply the tab index in the order I selected them. I know this is a featur

s3cmd to set the correct content type for image / jpeg

I have uploaded a number of directories that contain images, what is the correct way to modify the content-type to "image/jpeg" without having to re-upload the images? I tried: s3cmd modify s3://ccc-public/catalog/cropped/EP01L.jpg --add-header=

How do I set the dynamic link path for a shared library?

I want to compile a shared library with an .interp segment. #include <stdio.h> int foo(int argc, char** argv) { printf("Hello, world!\n"); return 0; } I'm using the following commands. gcc -c -o test.o test.c ld --dynamic-linker=blah -shar

How do I know the compatible java type for the ResultSet column type in runtime?

Following row returns concrete RDBMS type. This type depends on RDBMS. resultSet.getMetaData().getColumnTypeName(1) For example in my case it is VARCHAR Is it possible to know in runtime compatible java type ? In case of when row above returns VARCHA

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK