no throws statement in Thread.run()

M

manuel aldana

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?
 
M

manuel aldana

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.
 
M

Mason Bryant

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.");
}
}
}
 
S

scamhi

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.
 
V

VisionSet

manuel aldana said:
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.
 
M

Mason Bryant

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

(e-mail address removed) (Mason Bryant) wrote in message > class ThreadCallingClass {
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top