Java捕获来自swingworker的类中的异常

时间:2017-07-01 20:15:52

标签: java exception swingworker

好吧,我有这段代码:

SwingWorker<Void,Void> worker = new SwingWorker<Void, Void>() {
   @Override
   public Void doInBackground() {
       Connection checker = new Connection();
       do{
           checker.checkConnection();
       }while(true);
   }}

这是方法

  

checkConnection()

public void checkConnection(){
do{
    try{
        URL url = new URL("http://www.google.com");
        URLConnection uc = url.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
                    System.out.println("You're connected");
        }
        online = true;
    }catch(Exception noConnessione){
        System.out.println("No connection..."); 
        System.out.println("I'm going to try again in 30s...");
        robot.delay(30000);
        online = false;
    }
}while(online == false);}

好吧,如果没有连接,它就不会进入catch块,但是如果我使用相同的代码而不使用swingworker,它会进入。

我绝对需要摆动工具,所以我该如何解决?

2 个答案:

答案 0 :(得分:0)

似乎工作得很好

import javax.swing.SwingWorker;
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Simple {

  public static void main(String [] arg) {
    System.out.println("Starting worker");
    SwingWorker<Void,Void> worker = new SwingWorker<Void, Void>() {
      @Override
      public Void doInBackground() {
        Connection checker = new Connection();
        do{
          System.out.println("Running routine test");
          checker.checkConnection();
        }while(true);
      }
    };
    worker.execute();
    while(true);
  }
}

class Connection {
  boolean online = false;
  public void checkConnection() {
    System.out.println("Check connection loop");
    while(online == false) {
      try{
        URL url = new URL("http://yyy.google.com");
        URLConnection uc = url.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
                    System.out.println("You're connected");
        }
        online = true;
      }catch(Exception noConnessione){
        online = false;
      }
      if(online == false) {
        try {
          System.out.println("No connection...");
          System.out.println("I'm going to try again in 3s...");
          System.out.flush();
          Thread.sleep(3000);
        } catch(InterruptedException ex) {
          System.out.println("I was awaken");
        }
      }
    }
  }
}

执行

> javac Simple.java
> java Simple
Starting worker
Running routine test
Check connection loop
No connection...
I'm going to try again in 3s...
No connection...
I'm going to try again in 3s...
No connection...
I'm going to try again in 3s...
No connection...
I'm going to try again in 3s...
No connection...
I'm going to try again in 3s...

答案 1 :(得分:0)

看起来有一个未被捕获的异常/错误中止doInBackground方法。此异常将由Swingworker静默捕获,并且仅在调用get方法时报告。

get方法将阻止,直到SwingWorker终止,这不适用于上面的代码(永久循环)。所以调用应该是被覆盖的done方法中的模式:

SwingWorker<Void,Void> worker = new SwingWorker<Void, Void>() {
    @Override
    public Void doInBackground() throws Exception {
        ...
    }
    @Override
    public void done() {
        try {
            get();
        } catch (InterruptedException | ExecutionException ex) {
            ex.printStackTrace();
            // TODO
        }
    }
};

但请注意,done在事件调度线程上执行!

替代方案,在try-catch中包含doInBackground整个内容:

    @Override
    public Void doInBackground() throws Exception {
        try {
            ...
        } catch (Throwable ex) {
            ex.printStackTrace();
            throw ex;
        }
    }

不是解决方案,但应该有助于找到错误,因此希望解决方案