Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Capturing an Exception in a Runnable  RSS feed

 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Runnable that throws exceptions. I would like to throw these exceptions from the class containing the Runnable and from which it is called. An exception within a Runnable only goes into that Runnable's stack. Is there a way to capture it and handle it by throwing it from the enclosing class?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a callback. Crudely:



I know in another thread you were posting about an ExceptionListener interface. If you want to, you can make HostToARunnable implement ExceptionListener, and use ExceptionListener.exceptionThrown() as the callback -- I called my method reportException() instead, but you could use exceptionThrown(). There's no real advantage to using that interface here, and I think that's why you got blank stares in response to your previous questions.
 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I appreciate the example. This is giving me something to work with. I actually have several exceptions that can be thrown so I think I'll use a Vector; then if it's not empty, I'll throw each one out of the calling program up to where I want to handle it.
[ October 01, 2004: Message edited by: Betty Schwartz ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!