passing a thrown exception

A

Aryeh.Friedman

Lets say I have some method that basically says (this is a syntex
question so thats why I said basically):

public void assertError(Exception exception)
{
if (exception)
System.out.println("PASS");
else
System.out.println("FAIL");
}

and then need to do something like:

assertError(new file("bad file name")); // or anything else that will
throw an exception

how can I force the line that throws the exception to actual apss it to
assertError so I can see if their was an exception thrown or not.

--Aryeh
 
J

James Westby

Lets say I have some method that basically says (this is a syntex
question so thats why I said basically):

public void assertError(Exception exception)
{
if (exception)
System.out.println("PASS");
else
System.out.println("FAIL");
}

and then need to do something like:

assertError(new file("bad file name")); // or anything else that will
throw an exception

how can I force the line that throws the exception to actual apss it to
assertError so I can see if their was an exception thrown or not.

--Aryeh

If I understand what you're after correctly then this is my normal way
of acheiving it

try {
someMethodCallOrSimilarThatIWouldLikeToThrowAnException();
fail();
} catch (TheExceptionThatIWantToBeThrownHere e) {
pass();
}

I have seen it with the fail() after the catch block, which I think
would be more robust, but I haven't come across a situation that this
caused a problem in.

If you are doing lots of similar things then this can be wrapped up in a
method to simplify the test to a single line.

Hope this helps,


James
 
A

Aryeh.Friedman

Thanks... I was aware of the above techinque my question was how to
implement it as a single method (passing the statement that may or may
not throw an exception)

--Aryeh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top