Results 1 to 5 of 5
  1. #1
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Unhappy HELP: Method Invocation

    Hi All,

    i am using

    method.invoke method to invoke some methods

    For Eg:
    if ( i == 1 )
    // get the method fun1 ( ... ) in method
    method.invoke ( this, args... );
    if ( i == 2 )
    // get the method fun2 ( ... ) in method
    method.invoke ( this, diffArgs... );


    everything is working fine. It is calling the function , now i am throwing an user created Exception ( HMSException ) from the method fun1 ( ... ).

    but when i but a try catch block in

    try{
    method.invoke ( this, args... );
    }catch ( HMSException e ) {
    print ( "Never got Caught" );
    }

    i am unable to catch the exception.


    try{
    method.invoke ( this, args... );
    }catch ( Exception e ) {
    print ( "Now it catches the correct Exception means same message." );
    }

    It works fine now, but the game is i want to catch the Exception which i haev thrown not the JAVA Exception...

  2. #2
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    Make sure your HMSException is extending Exception.

  3. #3
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default thanks

    thanks for the reply.

    I have already extended the Exception class, but still it is of no use.

    it is like, i am using

    try{
    method.invoke ( ); // method points to my method which throws HMSException
    }catch ( Exception e ) { } // works fine


    try{
    method.invoke ( ); // method points to my method which throws HMSException
    }catch ( HMSException e ) { } // doesn' works


    Thanks in advance

  4. #4
    spikey is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default

    Your HMSException is not a checked exception on Method.invoke so you will be unable to catch it.

    If the invoking method (fun1) throws an exception, invoke with catch this and will in turn throw a InvocationTargetException with your HMSException as the cause.

    Hope that helps?

  5. #5
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default wooo

    OMG! sounds good, let me try this. :rolleyes:

    Thanks Spikey

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. Method Help
    By pringle in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 01:23 PM
  3. Replies: 2
    Last Post: 06-12-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •