Catching exceptions

Discussion in 'Java' started by Brenton Fletcher, Feb 12, 2004.

  1. Hello everybody,

    I have a constructor which starts a thread and I want the thread to throw an
    exception in it's run method and then for the constructor to throw the
    exception back to the caller of the constructor. Does anyone have any
    pointers on this matter ? The exception I am throwing is a checked
    exception. I know this might be impossible but I am interested in potential
    solutions.

    How do I reference a class located in a package in a .jar file?

    Brenton
     
    Brenton Fletcher, Feb 12, 2004
    #1
    1. Advertising

  2. Brenton Fletcher

    Adam Maass Guest

    "Brenton Fletcher" <> wrote:

    >
    > I have a constructor which starts a thread and I want the thread to throw

    an
    > exception in it's run method and then for the constructor to throw the
    > exception back to the caller of the constructor. Does anyone have any
    > pointers on this matter ? The exception I am throwing is a checked
    > exception. I know this might be impossible but I am interested in

    potential
    > solutions.


    Here's one (very rough) idea:

    class A{
    A() throws MyExc {
    MyRunnable r = new MyRunnable();
    Thread t = new Thread(r);
    t.start();
    t.join();

    MyExc e = r.getExc();
    if(e != null) throw e;
    }
    }

    class MyRunnable implements Runnable{
    MyExc exception = null;l

    public void run(){
    try{
    ....
    }
    catch(MyExc e){
    exception = e;
    }
    }

    MyExc getExc() { return exception; }
    }


    If anything the run method of MyRunnable calls throws an exception, the A
    constructor can retrieve it and throw it to its caller. But the thread that
    calls A's constructor waits for the new thread created in the constructor to
    die before it checks whether the new thread caused an exception; we've lost
    all of the benefit of an asychronous call and introduced a lot of complexity
    in the process.

    It shows up a more general problem, though: if you really want to throw an
    exception caused by a the run method of a thread out of the method that
    spawns that thread, you must: 1) find a way to communicate the exception
    from the spawned thread to the creating thread (one approach as I've shown
    you), and 2) find a way to make the creating thread wait long enough so that
    the spawned thread has a chance to throw any such interesting exception, and
    you must do this without losing what you were trying to gain by
    multithreading in the first place. (My code above is not a good model for
    this.)

    I would examine your reasons for wanting to propogate the exception up
    beyond the run method of the spawned thread. It might be simpler to
    implement some other error notification mechanism than to solve the two
    points I outlined above.

    >
    > How do I reference a class located in a package in a .jar file?


    Add the .jar file to your classpath; now the class is referenceable just
    like any other packaged class.
     
    Adam Maass, Feb 13, 2004
    #2
    1. Advertising

  3. Brenton Fletcher

    B Guest

    Hi,
    Thankyou for your advice.
    I have found a solution to my problem:
    I add an ActionListener to the thread. When the thread ends, it calls the
    actionPerformed() method of the ActionListener.
    The actionPerformed method checks to see if an exception has occuered in the
    thread. If so, it sets an instance variable to that exception.
    Since the program that uses my class has to call a certain method regularly,
    I just throw the exception in that method, thus notifing the program that
    the Exception occured - i.e.
    constructor
    {
    TheThread thread = new TheThread();
    class ThreadEndedListener implements ActionListener
    {
    public void actionPrformed(ActionEvent event)
    {
    theException = thread.getTheException();
    }
    }
    thread.addActionListener(new ThreadFinishedListener());
    }
    public void methodThatGetsCalledRegularly() throws Exception
    {
    if(theException != null)
    {
    throw theException;
    }
    }

    Thanks,
    Brenton
    "Adam Maass" <> wrote in message
    news:...
    >
    > "Brenton Fletcher" <> wrote:
    >
    > >
    > > I have a constructor which starts a thread and I want the thread to

    throw
    > an
    > > exception in it's run method and then for the constructor to throw the
    > > exception back to the caller of the constructor. Does anyone have any
    > > pointers on this matter ? The exception I am throwing is a checked
    > > exception. I know this might be impossible but I am interested in

    > potential
    > > solutions.

    >
    > Here's one (very rough) idea:
    >
    > class A{
    > A() throws MyExc {
    > MyRunnable r = new MyRunnable();
    > Thread t = new Thread(r);
    > t.start();
    > t.join();
    >
    > MyExc e = r.getExc();
    > if(e != null) throw e;
    > }
    > }
    >
    > class MyRunnable implements Runnable{
    > MyExc exception = null;l
    >
    > public void run(){
    > try{
    > ....
    > }
    > catch(MyExc e){
    > exception = e;
    > }
    > }
    >
    > MyExc getExc() { return exception; }
    > }
    >
    >
    > If anything the run method of MyRunnable calls throws an exception, the A
    > constructor can retrieve it and throw it to its caller. But the thread

    that
    > calls A's constructor waits for the new thread created in the constructor

    to
    > die before it checks whether the new thread caused an exception; we've

    lost
    > all of the benefit of an asychronous call and introduced a lot of

    complexity
    > in the process.
    >
    > It shows up a more general problem, though: if you really want to throw an
    > exception caused by a the run method of a thread out of the method that
    > spawns that thread, you must: 1) find a way to communicate the exception
    > from the spawned thread to the creating thread (one approach as I've shown
    > you), and 2) find a way to make the creating thread wait long enough so

    that
    > the spawned thread has a chance to throw any such interesting exception,

    and
    > you must do this without losing what you were trying to gain by
    > multithreading in the first place. (My code above is not a good model for
    > this.)
    >
    > I would examine your reasons for wanting to propogate the exception up
    > beyond the run method of the spawned thread. It might be simpler to
    > implement some other error notification mechanism than to solve the two
    > points I outlined above.
    >
    > >
    > > How do I reference a class located in a package in a .jar file?

    >
    > Add the .jar file to your classpath; now the class is referenceable just
    > like any other packaged class.
    >
    >
    >
     
    B, Feb 14, 2004
    #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. Marina
    Replies:
    2
    Views:
    513
    Marina
    Jul 8, 2003
  2. Amil Hanish
    Replies:
    0
    Views:
    569
    Amil Hanish
    Apr 13, 2006
  3. Adam Maass
    Replies:
    5
    Views:
    432
    Sudsy
    Jul 22, 2003
  4. Mike Schilling
    Replies:
    2
    Views:
    364
    Mike Schilling
    Jul 16, 2003
  5. Mick
    Replies:
    0
    Views:
    462
Loading...

Share This Page