Thread throwing an Exception

Discussion in 'Java' started by IveCal, Nov 9, 2006.

  1. IveCal

    IveCal Guest

    Hello, I got this code from David Reilly's site. What this prog does is
    terminate the prog after a specified time. What I want to do is just
    throw an Exception to be caught by other java programs. I tried to
    throw an exception in timeout (ex. timeout() throws Exception) and run
    (ex. run throws Exception) methods but it is not allowed. Please help.

    import java.net.*;

    public class Timer extends Thread
    {
    /** Rate at which timer is checked */
    protected int m_rate = 100;

    /** Length of timeout */
    private int m_length;

    /** Time elapsed */
    private int m_elapsed;

    /**
    * Creates a timer of a specified length
    * @param length Length of time before timeout occurs
    */
    public Timer ( int length )
    {
    // Assign to member variable
    m_length = length;

    // Set time elapsed
    m_elapsed = 0;
    }


    /** Resets the timer back to zero */
    public synchronized void reset()
    {
    m_elapsed = 0;
    }


    /** Performs timer specific code */
    public void run()
    {
    // Keep looping
    for (;;)
    {
    // Put the timer to sleep
    try
    {
    Thread.sleep(m_rate);
    }
    catch (InterruptedException ioe)
    {
    continue;
    }

    // Use 'synchronized' to prevent conflicts
    synchronized ( this )
    {
    // Increment time remaining
    m_elapsed += m_rate;

    // Check to see if the time has been exceeded
    if (m_elapsed > m_length)
    {
    // Trigger a timeout
    timeout();
    }
    }

    }
    }

    // Override this to provide custom functionality
    public void timeout()
    {
    System.err.println ("Network timeout occurred.... terminating " );
    System.exit(1);
    }
    }
     
    IveCal, Nov 9, 2006
    #1
    1. Advertising

  2. "IveCal" <> wrote in message
    news:...
    > Hello, I got this code from David Reilly's site. What this prog does is
    > terminate the prog after a specified time. What I want to do is just
    > throw an Exception to be caught by other java programs. I tried to
    > throw an exception in timeout (ex. timeout() throws Exception) and run
    > (ex. run throws Exception) methods but it is not allowed. Please help.


    You cannot throw an exception into another program or even another thread.
    You can, however, signal (or interrupt) the other thread when the exception
    or other condition appears in the timer and that signal can be used to throw
    an exception or make the other thread do something. You'll have to say more
    about what you're really trying to do--exceptions are probably not the way
    to accomplish it.

    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Nov 9, 2006
    #2
    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. KJ
    Replies:
    5
    Views:
    384
    John Saunders
    Jul 25, 2003
  2. VincentWong
    Replies:
    1
    Views:
    570
    VincentWong
    Dec 29, 2003
  3. =?Utf-8?B?VGVycnk=?=
    Replies:
    4
    Views:
    6,026
    =?Utf-8?B?VGVycnk=?=
    Jan 13, 2005
  4. =?Utf-8?B?R2xlbm4gVmVuemtl?=

    System.IO.Directoryinfo throwing exception

    =?Utf-8?B?R2xlbm4gVmVuemtl?=, Jul 25, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,718
    John Timney \(ASP.NET MVP\)
    Jul 25, 2005
  5. =?Utf-8?B?RXVzdGljZSBTY3J1YmI=?=

    Insert.aspx throwing exception

    =?Utf-8?B?RXVzdGljZSBTY3J1YmI=?=, Sep 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    581
    =?Utf-8?B?RXVzdGljZSBTY3J1YmI=?=
    Sep 23, 2005
Loading...

Share This Page