I need to throw an exception. The only issue is that the exception is thrown from a thread started from within the method.
Here is an example of what I mean:
public void connectToWebsite(String URL){
new Thread(){
@Override
public void run(){
openURLConnection(URL);//Throws Malformed URL and IO Exception
}
};
}
This is part of an API so I want to allow the users of the API to handle the exception how they so choose. However, I cannot get connectToWebsite(url)
to throw the exception as the exception is thrown within run. I cannot get run()
to throw the exception as then it would not be overriding the run method. Is there a way I can get the exception is thrown without building an entire new thread class? Or should I just settle on adding a catch with printStackTrace
s?
I would ideally like a connectToWebsite
to throw an exception and have the user handle it. However, is that even possible? If so, how? If I cannot or should not throw an Exception how should I notify the user something went wrong?