简体   繁体   English

一个人如何从线程抛出异常?

[英]How does one throw an exception from a thread?

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. 这是API的一部分,因此我希望允许API的用户处理他们如何选择的异常。 However, I cannot get connectToWebsite(url) to throw the exception as the exception is thrown within run. 但是,我无法通过connectToWebsite(url)引发异常,因为在运行时引发了异常。 I cannot get run() to throw the exception as then it would not be overriding the run method. 我无法让run()抛出异常,因为那样就不会覆盖run方法。 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? 还是我应该只考虑使用printStackTrace添加一个捕获?

I would ideally like a connectToWebsite to throw an exception and have the user handle it. 理想情况下,我希望connectToWebsite引发异常并让用户处理它。 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? 如果我不能或不应该抛出异常,应该如何通知用户出了什么问题?

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 printStackTraces? 还是我应该只考虑使用printStackTraces添加一个功能?

There are a couple of things you can do. 您可以做几件事。 The best is arguably to switch to use an ExecutorService and use a Callable instead of a runnable. 最好的办法是切换为使用ExecutorService并使用Callable而不是runnable。 You can submit your Callable to the ExecutorService which returns a Future . 您可以将Callable提交给ExecutorService ,该服务返回Future When you call future.get() it will throw an exception if your call() method threw an exception. 当您调用future.get() ,如果您的call()方法引发了异常,它将引发异常。 It actually throws ExecutionException and the cause of that exception will be the exception thrown by call() . 它实际上抛出ExecutionException并且该异常的原因将是call()抛出的异常。

You can also use an UncaughtExceptionHandler and set it on the Thread . 您还可以使用UncaughtExceptionHandler并将其设置在Thread Then if you thread throws, the handler will be called with the exception. 然后,如果线程抛出异常,则将调用异常处理程序。

Runnable cannot throw exception or return values. Runnable不能引发异常或返回值。

One thing which could be done is to let the object which is created by openURLConnection to null. 可以做的一件事就是让openURLConnection创建的openURLConnection为null。 So the users will check 因此用户将检查

if (object == null) { /* something went wrong with url connection */ }
else {
/* all ok */
}

and knows if something went wrong or not. 并知道是否出了问题。

It depends to how your API works. 这取决于您的API的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM