Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Catching exceptions

Reply
Thread Tools

Catching exceptions

 
 
Brenton Fletcher
Guest
Posts: n/a
 
      02-12-2004
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


 
Reply With Quote
 
 
 
 
Adam Maass
Guest
Posts: n/a
 
      02-13-2004

"Brenton Fletcher" <(E-Mail Removed)> 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.



 
Reply With Quote
 
 
 
 
B
Guest
Posts: n/a
 
      02-14-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Brenton Fletcher" <(E-Mail Removed)> 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.
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Catching unhandled exceptions using HttpModules Amil Hanish ASP .Net 0 04-12-2006 11:51 PM
Catching exceptions in JSP when they occur in Servlets Mick Java 0 08-06-2003 01:12 PM
Re: Catching exceptions that are never thrown Adam Maass Java 5 07-22-2003 10:58 PM
Re: Catching exceptions that are never thrown Mike Schilling Java 2 07-16-2003 08:36 PM
Re: catching exceptions from web user controls Marina ASP .Net 2 07-08-2003 04:48 PM



Advertisments