为什么会throw outerE;生成编译错误?我知道throw e;由于精确的重新抛出功能,不应该生成编译器错误.
它们是相同的Exception对象,但是一个catch仅限于块内部,一个作用于try-catch块之外.
这些都不应该生成编译器错误吗?或者,至少,两者的行为方式相同?
static void preciseRethrowTest()
{
Exception outerE;
try
{
}
catch (Exception e)
{
outerE = e;
// Compilation error here. Unhandled exception type Exception
// throw outerE;
throw e; // No compiler error
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Java 1.8.0_51.(精确的重新抛出在Java 7中引入)
不是试图比较语言,而只是为了知识,
有没有办法throws在Python中拥有相同的java 关键字/功能?
或者我们可以识别静态时间任何方法抛出的检查异常的方式?
或传递(链接)异常处理责任?
Java的:
public void someMethod() throws SomeException
{
}
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
@someDecorator # any way to do?
def someMethod():
pass
Run Code Online (Sandbox Code Playgroud) 当试图通过sbt "++2.11.6 publishLocal"或发布我的项目的jar时,我sbt +publishLocal在发布Scala 2.11.6时会遇到Scaladoc问题.看来我的@throws标签导致链接无效.我不知道为什么我有无效的链接,也不知道为什么在我的交叉发布期间Scala 2.11.6只出现错误而Scala 2.10.5出错.我找不到任何迹象表明@throwsScala 2.11上不支持该标记; 所以,我认为这是我的Scaladoc的一个问题,但我不知道我在这一点上缺少什么.谁能让我对这个问题有所了解?
/**
* Attempts to load the JDI, asserting that it can be and is loaded.
*
* @throws AssertionError If failed to load the JDI
*/
@throws(classOf[AssertionError])
protected def assertJdiLoaded(): Unit =
assert(jdiLoader.tryLoadJdi(),
"""
|Unable to load Java Debugger Interface! This is part of tools.jar
|provided by OpenJDK/Oracle JDK and is the core of the debugger! Please
|make sure that JAVA_HOME has been …Run Code Online (Sandbox Code Playgroud) 通常,Java编译器确认抛出的所有已检查异常都在throw规范中.当本机函数抛出不在函数抛出规范列表中的java检查异常时,或者在运行时是否忽略抛出规范列表时,是否会发生任何特殊情况?
C++
void function(JNIEnv * env, jclass jc) {
jclass newExcCls = env->FindClass("java/lang/NullPointerException");
env->ThrowNew(newExcCls, "ERROR");
}
Run Code Online (Sandbox Code Playgroud)
Java的
public class Tester {
static {
System.loadLibrary( "MyLibrary" );
}
private static native void function();
public static void main(String [ ] args) {
try {
function();
} catch( Exception e ) { //is it caught? Or what happens?
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
(C++函数名称可能会被破坏.另外loadLibrary也应该在try catch中.不关心,我不相信它与问题有关.代码中可能还有其他错误,但它们可能不相关无论是.)
我偶然发现代码看起来像这样:
void run() {
try {
doSomething();
} catch (Exception ex) {
System.out.println("Error: " + ex);
throw ex;
}
}
void doSomething() {
throw new RuntimeException();
}
Run Code Online (Sandbox Code Playgroud)
这段代码使我感到惊讶,因为它看起来像run()-method能够抛出an Exception,因为它可以捕获Exception然后重新抛出它,但是该方法未声明为throw Exception,显然不需要。这段代码可以很好地编译(至少在Java 11中)。
我的期望是我必须throws Exception在run()-method中声明。
额外的信息
以类似的方式,如果doSomething被声明为throw,IOException那么即使被捕获并重新抛出,也只IOException需要在run()-method中声明Exception。
void run() throws IOException {
try {
doSomething();
} catch (Exception ex) {
System.out.println("Error: " + ex);
throw ex;
}
}
void doSomething() throws …Run Code Online (Sandbox Code Playgroud) 在 C++20 (N4849) 中,关联容器extract()和insert(node_handle)/insert(hint, node_handle)方法没有异常安全措辞。
但是对于merge(),虽然有这样的措辞:
抛出:除非比较对象抛出,否则什么都没有。
位置:
22.2.6 关联容器 [associative.reqmts]
表 78:关联容器要求(除了容器)[tab:container.assoc.req]
第 799 页
显然,最初的提案 ( P0083R3 ) 旨在使其无投掷(第 5 页):
异常安全
如果容器的 Compare 函数是 no-throw(这是很常见的),那么移除、修改和插入节点都是 no-throw,除非修改值会抛出。如果修改值确实抛出,它会在所涉及的容器之外进行。
但为什么在该提案后面的拟议措辞中没有发言权?
在假设的情况下,我有一个这样的课:
import java.io.File;
import java.util.Scanner;
class X
{
static Scanner scanner;
static
{
scanner = new Scanner(new File("X.txt"));
}
}
Run Code Online (Sandbox Code Playgroud)
在编译时,我明白了
未报告的例外
java.io.FileNotFoundException; 必须被抓住或宣布被抛出
因为public Scanner(File source) throws FileNotFoundException.
要解决这个问题,我可以scanner = new...在try/catch语句中添加一行:
static
{
try
{
scanner = new Scanner(new File("X.txt"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我有什么方法可以做到这样的事情:
static throws java.io.FileNotFoundException
{
scanner = new Scanner(new File("X.txt"));
}
Run Code Online (Sandbox Code Playgroud)
这是一个假设的情况.请不要说"那你为什么要那样做?" 或者"这是制作扫描仪的更好方法!"
任何人都能用一个例子清楚地说明Java异常处理中throw和throws之间的区别吗?我试过谷歌搜索但无法得出结论.请帮忙
我在Kotlin中定义了一个函数:
fun convertExceptionToEmpty(requestFunc: () -> List<Widget>): Stream<Widget> {
try {
return requestFunc().stream()
} catch (th: Throwable) {
// Log the exception...
return Stream.empty()
}
}
Run Code Online (Sandbox Code Playgroud)
我已经使用此签名定义了一个Java方法:
List<Widget> getStaticWidgets() throws IOException;
Run Code Online (Sandbox Code Playgroud)
我试图像这样组成它们:
Stream<Widget> widgets = convertExceptionToEmpty(() -> getStaticWidgets())
Run Code Online (Sandbox Code Playgroud)
当我编译时,我收到此错误:
错误:(ln,col)java:未报告的异常java.io.IOException; 必须被抓住或宣布被抛出
如何定义我的函数参数以接受抛出的函数?
我最近发现可以在 Javadoc 中对同一个异常使用多个@throws标签。
我的一位学生用它来记录他在《四子棋》中的一种方法:
/*
* ...
* @throws IllegalArgumentException if the number of rows or columns is invalid
* @throws IllegalArgumentException if one of the players has {@link Stone#None} as stone
* @throws IllegalStateException if both players use the same stone color
*/
public void doSomething(...) { ... }
Run Code Online (Sandbox Code Playgroud)
现在我(和他)的问题是:对于是否使用单个@throws标签或“是否可以”为每个异常类型使用多个标签,是否有官方风格指南或一般建议?