atm I am using an inner thread for calling a method. This method can throw an Exception. Is there a way to receive the Exception in the outer class to react to it?
Or should I go with writing a “workthread” and adding an observer to it?
I implemented an MVC-Pattern. This this method is called in my model. Now I want to display an msg about the exception. Therefore I need to know the exception.
public void startServer(final String path, final int port, final double speedup) {
serverIsStopped = false;
new Thread() {
public void run() {
server = new SWTimeServer();
try{
server.startServer(port, speedup, path);
}catch (ClientDisconnectedException e) {
serverIsStopped = true;
//TODO
} catch (ParseException e) {
serverIsStopped = true;
//TODO
}
}
}.start();
}
I came up with this quick solution. But pretty ugly. Your opinions?
private boolean serverIsStopped = true;
private Model model = this;
public void startServer(final String path, final int port, final double speedup) {
serverIsStopped = false;
new Thread() {
public void run() {
server = new SWTimeServer();
try{
server.startServer(port, speedup, path);
}catch (ClientDisconnectedException e) {
serverIsStopped = true;
model.notifyObservers(e);
} catch (ParseException e) {
serverIsStopped = true;
model.notifyObservers(e);
}
}
}.start();
}
Thanks for answers
Greetings
Tarken
You can’t receive an exception from Thread A from Thread B with any sort of basic try/catch structure that’d you’d normally use. You’ll have to implement some type inter-thread messaging/signaling if you want to do something like that.
Beyond that advice, there’s not much help I can give without a more complete explanation of what exactly you’re trying to do.