Throws exception into Threads

Discussion in 'Java' started by Fran Garcia, Dec 13, 2004.

  1. Fran Garcia

    Fran Garcia Guest

    I´ve developped an applet that run Threads and I wanna handle the
    errors throwing exceptions. I´ve readen a lot of post and they comment
    that is impossible make it. But I´ve not found a good solution. Can
    anybody help me?

    Any suggestion would be appreciated!

    Best regards
     
    Fran Garcia, Dec 13, 2004
    #1
    1. Advertising

  2. I would use a variable in the thread to let the threads communicate
    possible exceptions:

    class MyThread extends Thread
    {
    private Throwable exception = null;

    public Throwable getException()
    {
    return exception;
    }

    public void run()
    {
    try
    {
    // do things
    }
    catch ( Throwable t )
    {
    exception = t;
    }
    }
    }

    public void foo()
    {
    MyThread t = new MyThread();

    t.run();
    t.join(); // or whatever is needed to know
    //that the thread is terminated
    t.sleep();
    if ( t.exception() != null )
    // error handling
    }


    Fran Garcia wrote:
    > I´ve developped an applet that run Threads and I wanna handle the
    > errors throwing exceptions. I´ve readen a lot of post and they comment
    > that is impossible make it. But I´ve not found a good solution. Can
    > anybody help me?
    >
    > Any suggestion would be appreciated!
    >
    > Best regards
     
    Andrea Desole, Dec 13, 2004
    #2
    1. Advertising

  3. On 13 Dec 2004 09:25:49 -0800, Fran Garcia <> wrote:

    > I´ve developped an applet that run Threads and I wanna handle the
    > errors throwing exceptions. I´ve readen a lot of post and they comment
    > that is impossible make it. But I´ve not found a good solution. Can
    > anybody help me?
    >
    > Any suggestion would be appreciated!


    Disclaimer: I am not sure i understood what you want to do.

    If you do mean what i think you mean, then indeed it is not possible.
    You can't "push" an Exception into another Thread of execution. All
    Exceptions never leave their originating Thread.

    If you want to use the Exceptions to signal the Threads that they
    should stop working, you would do much better to check some
    "chancel" field in the Runnable every so often, and exit the
    thread if it ever becomes "true"

    --

    Whom the gods wish to destroy they first call promising.
     
    Stefan Schulz, Dec 13, 2004
    #3
  4. Fran Garcia wrote:

    > I´ve developped an applet that run Threads and I wanna handle the
    > errors throwing exceptions. I´ve readen a lot of post and they comment
    > that is impossible make it. But I´ve not found a good solution. Can
    > anybody help me?


    Much depends on what you mean to do with such exceptions. One
    alternative is to create a subclass of ThreadGroup that overrides the
    uncaughtException() method (compatibly!) so that it does something
    useful to you. You then assign the threads created by your applet to an
    instance of this ThreadGroup. Do note that the uncaughtException method
    will be invoked by some unspecified thread in the JVM, which is probably
    not the thread wherein the exception was thrown, and surely not the
    applet's main thread (inasmuch as it has one). All that assumes you
    mean to do something different than handle exceptions by making
    appropriate use of Java's try / catch mechanism within the context of a
    single thread.

    If any of that fails to make sense to you, then you would be best off to
    study some of the basics of programming with threads, and possibly the
    basics of using exceptions. Writing correct multithreaded programs is
    tricky -- easier in Java than in some languages, but tricky anywhere.
    If you don't have a firm grasp of the language basics before you start
    in to work with threads then you are toast. Crispy brown toast.


    John Bollinger
     
    John C. Bollinger, Dec 13, 2004
    #4
    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. Himmat Dhange
    Replies:
    0
    Views:
    433
    Himmat Dhange
    Aug 26, 2003
  2. Cowboy \(Gregory A. Beamer\)
    Replies:
    1
    Views:
    805
    =?Utf-8?B?amhpbGw=?=
    May 16, 2004
  3. Aren Cambre
    Replies:
    5
    Views:
    4,103
  4. Chris Miller
    Replies:
    4
    Views:
    13,363
    Chris Smith
    Nov 22, 2003
  5. Replies:
    2
    Views:
    335
Loading...

Share This Page