问题标签 [checked-exceptions]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
223 浏览

java - 除了传播检查的异常之外,还有其他的 throws 角色吗?

在研究了越来越多关于throws声明的内容后,Exception Handling我感到困惑。我发现-

如果方法能够导致它无法处理的异常,则它必须指定此行为,以便方法的调用者可以保护自己免受该异常的影响。

而不是使用 throws 仍然存在异常-我的控制台显示以下错误-

直到我没有在 try-catch 块中调用 meth1,尽管使用了 throws,但仍然存在异常。throws在这里的作用是什么?

我需要你的确认。我很困惑。我的单行查询是-

throws除了传播 a之外,还有其他角色CheckedException吗?

0 投票
1 回答
2091 浏览

java - 标准 API 中所有已检查/未检查异常的参考

我正在尝试了解 Java 中的异常,并且出于教育目的,如果我能获得标准 API 中所有已检查异常和所有未检查异常的引用,那就太好了。

相关问题,但不完全是我所追求的。

0 投票
1 回答
66 浏览

java - ActionListener 类中的散列字符串 (SHA-256)

我想在一个ActionListener类中使用以下代码。

但是"SHA-256"and"UTF-8"不能以任何方式导入。当我在控制台程序中执行此操作时,我可以通过以下方式解决此问题:

但是我不能ActionListener上课。我该如何解决这个问题?

0 投票
1 回答
1568 浏览

java - 已检查的异常不会在链中传播

为什么检查的异常不会在链中传播?

为什么要处理所有已检查的异常?

0 投票
1 回答
194 浏览

scala - 不清楚“Scala 中的函数式编程”中的“Checked exception”解释

在《Scala 中的函数式编程》一书中,有一段话讲到了“受检异常”:

已检查的异常

Java 的检查异常至少会强制决定是否处理或重新引发错误,但它们会导致调用者的大量样板文件。更重要的是,它们不适用于高阶函数,它们不可能知道它们的参数可能引发的特定异常。例如,考虑我们为 List 定义的 map 函数:

def map[A,B](l: List[A])(f: A => B): List[B]

这个函数显然很有用,高度通用,并且与检查异常的使用不一致——我们不能为每个可能由f. 即使我们想这样做,map 怎么会知道哪些异常是可能的呢?这就是为什么通用代码,即使在 Java 中,也经常求助于使用 RuntimeException 或一些常见的检查异常类型。

这部分我读了好几遍,但仍然不清楚为什么受检异常不适用于高阶函数

有人可以举一些例子让它更清楚吗?

0 投票
3 回答
10279 浏览

java - 从 lambda 抛出异常

鉴于此 java 8 代码

我们如何正确地将IOException其委托给方法调用的堆栈?(简而言之,如何让这个方法抛出这个IOException?)

java中的Lambdas看起来对错误处理不是很友好......

0 投票
5 回答
1831 浏览

java - 使用 final 成员处理构造函数中捕获的 Java 异常

我有一些丑陋的代码,想重构它:

导致丑陋的问题是final成员之间的交互和捕获的异常。如果可能的话,我想保留成员final

我想按如下方式编写代码,但 Java 编译器无法分析控制流 -address无法再次分配,因为必须抛出第一次尝试分配才能使控制到达catch子句。

Error:(27, 13) error: variable address might already have been assigned

有什么建议吗?

PS我有一个约束,即构造函数不会抛出 - 否则这一切都很容易。

0 投票
1 回答
379 浏览

java - Should user input cause checked exceptions?

I have an operation/method that performs an insert to a database. It takes several fields and for various reasons that operation could fail because one or more of the inputs was not unique or because they conflict with some internal records that need to be unique.

Would it make sense for me to throw a checked exception on each violation?

My thinking was to use the following:

forcing any developer that calls this method to deal with it by routing the web user to a page asking for a new token.

Opposition The opponents of this approach point out that we, the dev team, are aware of each of the error cases that would cause this to fail and should instead return an error code and use that to handle each case.

I don't see any obvious drawbacks to the checked approach. These error cases are very likely to happen and you absolutely must address them anywhere saveUserInfo() is used. It seems like exactly the situation checked exceptions were created for.

0 投票
3 回答
2408 浏览

java - Java 编译时检查异常

Exception两者IOException都是编译时检查的异常。

但是,我们不能IOException在 catch 块中使用。但是我们可以Exception在 catch 块内使用它的原因是什么。

0 投票
7 回答
8152 浏览

java - Java中的异常类型

我对 Java 中的异常类型感到困惑。在许多教程网站上,我看到 java 中有两种类型的异常

  1. 编译时异常
  2. 运行时异常

但是当我和一些java高手交谈时,据他们说没有编译时异常之类的东西。他们说这是编译时错误而不是异常,而且我在Java 文档中没有发现任何关于编译时异常的信息。但是当我运行以下程序时

如果未提供 try catch,我得到以下输出。

那么我应该考虑这个错误还是编译时异常?