gpt4 book ai didi

java - 当使用 throws 关键字调用 void 时,所有抛出的错误都适用于我调用 void 的地方

转载 作者:行者123 更新时间:2023-12-02 03:32:38 25 4
gpt4 key购买 nike

我目前正在尝试用java制作一个换档 key 扣,使用重音作为切换开关,而机器人是我发现这非常简单的唯一方法。该程序执行顾名思义的操作,并为我按住 Shift 键。如果你想知道这个程序在哪里有用,那么为星界建筑制作程序,因为有大写锁定。然而,当使用机器人时,我必须抛出一些异常,但它们随后出现在我调用 void 的地方

我找不到任何解决方案,可能是因为我正在使用 JNativeHook 库创建一个会对坟墓符号使用react的键盘记录器。我还是个新手,我从网上的片段中将很多内容拼凑在一起。

这就是我的错误的来源,完整的程序可用 here我会发布一个最小的可重复示例,但我不能,因为我不知道哪些部分对于这一部分的功能至关重要。

//tried to put throws in nativeKeyPressed to get rid of the exceptions but then that gives me an exception saying i can't do that because it causes clashing
public void nativeKeyPressed(NativeKeyEvent e){
System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

if (e.getKeyCode() == NativeKeyEvent.VC_BACKQUOTE) {
toggle(); //get IOException, AWTException, and InterruptedException whenever i put throws on my toggle() void
}
}

//throw exceptions so robot has no conflicts
public static void toggle() throws IOException, AWTException, InterruptedException{
bool = !bool;
Robot r = new Robot();

if (bool = true) {

} else if (bool = false) {

}

我希望它没有错误,但是 Robot()如果我不抛出异常,则有异常,并且 toggle();有任何我从 public static void toggle() 抛出的异常。我对机器人以外的方法持开放态度,但我找不到其他方法。

最佳答案

除非您期望任何这些异常并且知道如何处理它们,否则只需包装您的异常即可

public static void rethrowException (Exception ex) {
throw new RuntimeException(ex);
}
//throw exceptions so robot has no conflicts
public static void toggle() {
try {
bool = !bool;
Robot r = new Robot();

if (bool = true) {

} else if (bool = false) {

}
} catch (IOException|AWTException|InterruptedException ex) {
throw new RuntimeException(ex);
}
}

关于java - 当使用 throws 关键字调用 void 时,所有抛出的错误都适用于我调用 void 的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56876477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com