Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Catching exceptions (http://www.velocityreviews.com/forums/t131311-catching-exceptions.html)

Brenton Fletcher 02-12-2004 11:11 PM

Catching exceptions
 
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



Adam Maass 02-13-2004 05:43 AM

Re: Catching exceptions
 

"Brenton Fletcher" <impactbc@hotmail.com> 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 02-14-2004 02:24 AM

Re: Catching exceptions
 
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" <adam.nospam.maass@comcast.net> wrote in message
news:U8udneX3t9yT-bHdRVn-sw@comcast.com...
>
> "Brenton Fletcher" <impactbc@hotmail.com> 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.
>
>
>





All times are GMT. The time now is 04:05 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57