Home > Java > javaTutorial > Why Can't I Catch an `ArrayIndexOutOfBoundsException` from a `Future` and `SwingWorker` when Using an `Executor`?

Why Can't I Catch an `ArrayIndexOutOfBoundsException` from a `Future` and `SwingWorker` when Using an `Executor`?

Patricia Arquette
Release: 2024-12-17 01:12:24
Original
464 people have browsed it

Why Can't I Catch an `ArrayIndexOutOfBoundsException` from a `Future` and `SwingWorker` when Using an `Executor`?

Unable to catch ArrayIndexOutOfBoundsException from Future and SwingWorker if thread starts Executor

Problem:
I am using Executor for SwingWorker incorrectly identifies the wrong elements from a Vector when multi-threaded, it looks like this code is rather ignoring elements that are not present in the Vector.

Problem details:

  • How did I accidentally ignore non-existent elements in a Vector?
  • How to catch such exception?

Problem output:

运行:
线程状态,名称:StartShedule,SwingWorker 状态为 STARTED
线程状态,名称:StartShedule,SwingWorker 状态为 DONE
线程状态,名称:StartShedule,SwingWorker 状态为 STARTED
线程状态,名称:StartShedule,SwingWorker 状态为 DONE
线程状态,名称:StartShedule,SwingWorker 状态为 STARTED
线程状态,名称:StartShedule,SwingWorker 状态为 DONE
构建成功(总时间:11 秒)
Copy after login

Exception raised by uncommenting the following code:

// 更改 TableValues1(); // 取消注释以获取 ArrayIndexOutOfBoundsException
Copy after login

Modified output:

运行:
线程“AWT-EventQueue-0”中出现异常java.lang.ArrayIndexOutOfBoundsException:数组索引超出范围:2
       在 java.util.Vector.get(Vector.java:694)
       在 KondorExport.Util.Help.Table.TableWithExecutor.changeTableValues1(TableWithExecutor.java:70)
       在 KondorExport.Util.Help.Table.TableWithExecutor.access0(TableWithExecutor.java:18)
       在 KondorExport.Util.Help.Table.TableWithExecutor.actionPerformed(TableWithExecutor.java:61)
       在 javax.swing.Timer.fireActionPerformed(Timer.java:271)
       在 javax.swing.Timer$DoPostEvent.run(Timer.java:201)
       在 java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
       在 java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
       在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
       在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
       在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
       在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
       在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
       在 java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
线程状态,名称:StartShedule,SwingWorker 状态为 STARTED
线程状态,名称:StartShedule,SwingWorker 状态为 DONE
构建成功(总时间:10 秒)
Copy after login

Code snippet:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.swing.*;
import javax.swing.table.*;

public class TableWithExecutor extends JFrame {

    // ... 代码 ...

    private void changeTableValues1() {
        fwDeals1 = new Vector<String>();
        fwDeals1.add("First"); // ElementAt(0)
        fwDeals1.add("Second");// ElementAt(1)
        checkDealsInDb1(fwDeals1.get(1), fwDeals1.get(2));
    }

    // ... 代码 ...
}
Copy after login

Edit:
by in I added the rethrowing of exceptions in the done() method in Future#get() (thanks to @takteek for the great suggestion).

@Override
    protected void done() {
        try {
            get();
            // errLabel.setText(String.valueOf(get()));
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        } catch (ExecutionException ee) {
            ee.printStackTrace();
        } catch (IllegalStateException is) {
            is.printStackTrace();
        }
    }
Copy after login

But the output is still only:

运行:
Got exception
线程状态,名称:StartShedule,SwingWorker 状态为 STARTED
线程状态,名称:StartShedule,SwingWorker 状态为 DONE
Got exception
线程状态,名称:StartShedule,SwingWorker 状态为 STARTED
线程状态,名称:StartShedule,SwingWorker 状态为 DONE
Got exception
线程状态,名称:StartShedule,SwingWorker 状态为 STARTED
线程状态,名称:StartShedule,SwingWorker 状态为 DONE
Got exception
线程状态,名称:StartShedule,SwingWorker 状态为 STARTED
线程状态,名称:StartShedule,SwingWorker 状态为 DONE
构建成功(总时间:13 秒)
Copy after login

The above is the detailed content of Why Can't I Catch an `ArrayIndexOutOfBoundsException` from a `Future` and `SwingWorker` when Using an `Executor`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template