passing a thrown exception

Discussion in 'Java' started by Aryeh.Friedman@gmail.com, Jan 13, 2006.

  1. Guest

    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
    , Jan 13, 2006
    #1
    1. Advertising

  2. James Westby Guest

    wrote:
    > 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
    James Westby, Jan 13, 2006
    #2
    1. Advertising

  3. Guest

    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
    , Jan 13, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom
    Replies:
    1
    Views:
    423
    Munsifali Rashid
    Dec 11, 2003
  2. Selen
    Replies:
    0
    Views:
    2,674
    Selen
    May 28, 2004
  3. =?Utf-8?B?VmFs?=
    Replies:
    0
    Views:
    3,122
    =?Utf-8?B?VmFs?=
    Jun 8, 2005
  4. jobs
    Replies:
    1
    Views:
    1,876
    Scott Roberts
    Nov 16, 2007
  5. Val
    Replies:
    0
    Views:
    174
Loading...

Share This Page