no throws statement in Thread.run()

Discussion in 'Java' started by manuel aldana, May 4, 2004.

  1. i tried to do

    class BlaThread extends Thread{

    public void run throws MyException(){
    .........
    .......
    throw new MyException();
    .....
    }


    is not possible to compile. why does java not allow to override a method
    with a different throws statement?
    or am i doing something wrong here?


    --
    manuel aldana

    e-mail adress not valid
    --> use aldana[at]cs.tu-berlin.de
    manuel aldana, May 4, 2004
    #1
    1. Advertising

  2. VisionSet wrote:
    [..]
    > Otherwise a subclass could be referenced by its superclass and throw
    > exceptions that were not specified by the superclass.


    yeah, i see this good point.
    thanks.


    --
    manuel aldana

    e-mail adress not valid
    --> use aldana[at]cs.tu-berlin.de
    manuel aldana, May 4, 2004
    #2
    1. Advertising

  3. manuel aldana

    Mason Bryant Guest

    The problem here is who catches the exception and where?

    for example, suppose you have some code like this:

    BlaThread l_bt = new BlaThread();

    l_bt.start();

    .... do some stuff...

    As soon as you call start(), your thread starts and some stuff
    happens.
    If an exception happens in your run method it could happen at any
    time. So you might be a couple lines after your l_bt.start() or you
    could be hundreds of lines away from it.

    What you might want to do is something like this (warning: Haven't
    debugged or even compiled this):

    class ThreadCallingClass {

    public void threadStarter() {
    BlaThread l_bt = new BlaThread(this);
    l_bt.start();

    }

    public void errorHandler(Exception e) {

    //notify the user that your thread has died and take care of
    cleanup.
    }
    }

    Class BlaThread
    ThreadCallingClass m_caller;
    BlaThread (ThreadCallingClass c) {
    m_caller = c;
    }

    public void run() {

    ...do some stuff...
    if(error)
    m_caller.errorHandler(new myException("Something bad
    happened.");
    }
    }
    }

    manuel aldana <> wrote in message news:<c788ul$rv1h$-berlin.de>...
    > i tried to do
    >
    > class BlaThread extends Thread{
    >
    > public void run throws MyException(){
    > ........
    > ......
    > throw new MyException();
    > ....
    > }
    >
    >
    > is not possible to compile. why does java not allow to override a method
    > with a different throws statement?
    > or am i doing something wrong here?
    Mason Bryant, May 4, 2004
    #3
  4. manuel aldana

    Guest

    "VisionSet" <> wrote in message news:<lNNlc.50$>...
    > "manuel aldana" <> wrote in message
    > news:c788ul$rv1h$-berlin.de...
    > > i tried to do
    > >
    > > class BlaThread extends Thread{
    > >
    > > public void run throws MyException(){
    > > ........
    > > ......
    > > throw new MyException();
    > > ....
    > > }
    > >
    > >
    > > is not possible to compile. why does java not allow to override a method
    > > with a different throws statement?
    > > or am i doing something wrong here?
    > >

    >


    To specifically answer your question, if you are calling code which throws
    exceptions in run(), you can throw a new RuntimeException and say initCause
    to your original exception. You can then have a ThreadGroup which has an
    overridable method uncaughtException which you can put your exception logging
    code into. You can say Throwable.getCause to get back to the underlying
    exception you stuck into the RuntimeException in the first place.
    , May 4, 2004
    #4
  5. manuel aldana

    VisionSet Guest

    "manuel aldana" <> wrote in message
    news:c788ul$rv1h$-berlin.de...
    > i tried to do
    >
    > class BlaThread extends Thread{
    >
    > public void run throws MyException(){
    > ........
    > ......
    > throw new MyException();
    > ....
    > }
    >
    >
    > is not possible to compile. why does java not allow to override a method
    > with a different throws statement?
    > or am i doing something wrong here?
    >


    Because that is a rule of the language.
    You cannot override a method to throw an exception that is not declared to
    be thrown by the method it overrides.
    To be more specific the new method must declare that it throws a checked
    exception that is assignment compatible with the overriden methods declared
    exception.

    Otherwise a subclass could be referenced by its superclass and throw
    exceptions that were not specified by the superclass.

    --
    Mike W
    VisionSet, May 4, 2004
    #5
  6. manuel aldana

    Mason Bryant Guest

    At the risk of stating the obvious, please make sure your errorHandler
    is threadsafe (probably by being synchronized)

    (Mason Bryant) wrote in message > class ThreadCallingClass {
    >
    > public void threadStarter() {
    > BlaThread l_bt = new BlaThread(this);
    > l_bt.start();
    >
    > }
    >
    > public void errorHandler(Exception e) {
    >
    > //notify the user that your thread has died and take care of
    > cleanup.
    > }
    > }
    >
    > Class BlaThread
    > ThreadCallingClass m_caller;
    > BlaThread (ThreadCallingClass c) {
    > m_caller = c;
    > }
    >
    > public void run() {
    >
    > ...do some stuff...
    > if(error)
    > m_caller.errorHandler(new myException("Something bad
    > happened.");
    > }
    > }
    > }
    Mason Bryant, May 5, 2004
    #6
    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. Chris Miller
    Replies:
    4
    Views:
    13,344
    Chris Smith
    Nov 22, 2003
  2. Jeffrey Barish
    Replies:
    0
    Views:
    302
    Jeffrey Barish
    May 28, 2004
  3. Replies:
    5
    Views:
    333
    Tim Golden
    Mar 15, 2007
  4. Replies:
    4
    Views:
    320
  5. Ralph Shnelvar
    Replies:
    3
    Views:
    106
    Ralph Shnelvar
    Sep 22, 2010
Loading...

Share This Page