有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何处理JavaBeanTigerProperty中的异常

我的项目分为两个模块:

模型中模块: 我有一个类SudokuField,它在更改时通知SudokuBoard。然后,如果启用了提示,如果无法使用插入值求解数独,则SudokuBoard会抛出带有一些信息的SudokuException

SudokuField通知:

public class SudokuField {

    private int value = 0;
    private PropertyChangeSupport notifier;

     public void setValue(int value) {

        if (value < 0 || value > 9) {
            throw new IndexOutOfBoundsException("Number: " + value + "Expected 0-9");
        }

        int oldValue = this.value;
        this.value = value;
        notifier.firePropertyChange("value", oldValue, value);

    }
}

SudokuBoard投掷:

public class SudokuBoard implements PropertyChangeListener{

    public static final int SIZE = 9;

    @Override
    public void propertyChange(PropertyChangeEvent evt) throws SudokuException {
        if (!autoCheck) {
            return;
        }
        if (checkBoard()) {
            return;
        }

        int oldValue = (int) evt.getOldValue();
        int newValue = (int) evt.getNewValue();

        SudokuField field = (SudokuField) evt.getSource();
        int i=0, j=0;
        outerloop:
        for (i = 0; i < SudokuBoard.SIZE; i++) {
            for (j = 0; j < SudokuBoard.SIZE; j++) {
                if (this.getField(i, j) == field ) {
                    break outerloop;
                }
            }
        }
        
        throw new SudokuException(
                "Wrong value inserted"
                        + "(old: " + oldValue
                        + ", new: " + newValue + ")",
                        i, j, oldValue, newValue, field);

    }
}

视图中模块: 我正在使用JavaFX和JavaBean来显示和同步SudokuFieldTextField。 我的控制器的一部分:

public class Window implements Initializable {

    @FXML
    private GridPane sudokuPane;

    private final SudokuBoard board = new SudokuBoard(new BacktrackingSudokuSolver());
    private final List<List<TextField>> fields = Arrays.asList(new List[9]);
    private final List<JavaBeanIntegerProperty> integerProperties =
            new ArrayList<JavaBeanIntegerProperty>();
    
    public Window() {
        for (int i = 0; i < SudokuBoard.SIZE; i++) {
            fields.set(i, Arrays.asList(new TextField[9]));
            for (int j = 0; j < SudokuBoard.SIZE; j++) {
                TextField textField = new TextField();
                fields.get(i).set(j, textField);
            }
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        StringConverter converter = new IntegerStringConverter();

        for (int i = 0; i < SudokuBoard.SIZE; i++) {
            for (int j = 0; j < SudokuBoard.SIZE; j++) {
                //I generate Sudoku board and TextFields in Constructor
                TextField textField = getTextField(i, j);
                SudokuField field = board.getField(i, j);

                JavaBeanIntegerPropertyBuilder builder = JavaBeanIntegerPropertyBuilder.create();
                JavaBeanIntegerProperty integerProperty = null;
                try {
                    integerProperty = builder.bean(field).name("value").build();
                } catch (NoSuchMethodException e) {
                    System.out.println("Ignoring NoSuchMethodException in " + this.getClass());
                }

                //i need to store JavaBeanIntegerProperty, otherwise GarbageCollector
                //destroys my connections
                integerProperties.add(integerProperty);

                //adding textField to GridPane
                sudokuPane.add(textField, i, j);
                textField.textProperty().bindBidirectional(integerProperty, converter);
            }
        }
    }
}

现在的大问题是:如何处理控制器中的那些SudokuExceptions?从这个异常中,我可以很容易地获得任何信息来修改我想要的文本字段,但是我无法处理它。我试着加上:

Thread.setDefaultUncaughtExceptionHandler(Window::exceptionHandler);
//and
Thread.currentThread().setDefaultUncaughtExceptionHandler(Window::exceptionHandler);

Window控制器中的exceptionHandler方法:

    public static void exceptionHandler(Thread thread, Throwable throwable) {

        for (int i = 0; i < 100; i++) {
            System.out.println("working: " + i);
        }

    }

在控制器的initialize和构造函数中,以及在mainstart方法中的视图模块的Main类中,但它没有调用此方法(我用System.out.println检查了这一点)。我不想将PropertyChangeListener添加到SudokuBoard,因为我想对我的模型模块进行尽可能少的更改

整个项目是作为我大学课程的一部分开发的项目


共 (1) 个答案

  1. # 1 楼答案

    好的,所以我找到了这个bug report,我想因为这个,我不能做我想要的。因此,我创建了自己的ActionEvent,它被“抛出”而不是SudokuBoard中的SudokuException,并向我的控制器添加了ActionEventListener接口。也许这不是我真正想要的解决方案,但我认为它更漂亮。特别感谢我的朋友帮助我。我将保留这个问题,因为可能有人知道如何绕过这个异常处理问题,其他人可以使用这个答案