简体   繁体   English

方法签名包括throws异常?

[英]Method signature including the throws exception?

I know the method signature is including method name and its parameter list . 我知道方法签名包括方法名称及其参数列表

But how about throws Exception ? 但是throws Exception呢?

public List<ServiceStatusVo> listServiceStatuses() throws RetrieverException {
    ...
    return list;
}

If it's not included then why I cannot pass in the following lambda: 如果不包括在内,那么为什么我不能传递以下lambda:

() -> listServiceStatuses()

but I can pass in 但我可以通过

() -> {
    try {
        return listServiceStatuses();
    } catch (RetrieverException e) {
    }
}

And also I can throw it out again 我也可以扔掉

() -> {
    try {
        return listServiceStatuses();
    } catch (RetrieverException e) {
        throw e;
    }
}

I know the Supplier<T> functional interface, that's what really confusing me if throws is not part of the method signature . 我知道Supplier<T>功能接口, 如果 throw不是方法签名的一部分,那真的让我感到困惑。

Thanks for the help. 谢谢您的帮助。

It's not about the method signature directly. 这与方法签名无关。 From JLS Sec 11.2.3 : JLS Sec 11.2.3开始

It is a compile-time error if a lambda body can throw some exception class E when E is a checked exception class and E is not a subclass of some class declared in the throws clause of the function type targeted by the lambda expression. 当E是检查的异常并且 E 不是 lambda表达式所针对的函数类型的throws子句中声明的某个类的子类时 ,如果lambda主体可以抛出某些异常类E,则是编译时错误。

This is a little surprising - I must admit that my initial thought was that the exception is part of the method signature. 这有点令人惊讶-我必须承认,我最初的想法是该异常方法签名的一部分。

But remember that "checked exception" means compile-time checked exception : the compiler makes sure that you have handled all checked exceptions; 但是请记住,“已检查的异常”是指编译时已检查的异常 :编译器确保已处理所有已检查的异常。 but once it has been compiled, checked and unchecked exception types are treated just the same. 但是一旦编译完成,已检查和未检查的异常类型将被视为相同。 Notice that that the JVM spec doesn't even mention checkedness in the section on exceptions. 请注意, JVM规范甚至在异常部分中都没有提到检查性。

So, as seen at runtime, the method can throw any exception. 因此,如在运行时所见,该方法可以引发任何异常。 And as stated in the language spec: 并且如语言规范中所述:

Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any) (§8.4.4), and, after adapting the formal parameter types of N to the the type parameters of M, the same formal parameter types. 如果两个方法或构造函数M和N具有相同的名称,相同的类型参数(如果有的话)(第8.4.4节),并且在将N的形式参数类型调整为类型参数之后,则具有相同的签名。 M,形式参数类型相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 throws x extends Exception方法签名 - throws x extends Exception method signature 由于没有方法签名,Artifactory.createClient()为JFrog Artifactory引发了异常 - Artifactory.createClient() throws exception for JFrog Artifactory due to no method signature 如果我们在方法签名中抛出子类型异常,是否可以在方法中抛出Parent类型异常? - Can we throw Parent type exception in the method if we written throws child type exception in the method signature? 使用@ControllerAdvice处理Spring Exception提供了集中控制,但是我们必须在方法签名中添加所有throws异常 - Spring Exception handling with @ControllerAdvice provides centralized control but we have to add all throws exception in method signature 接口中方法签名的异常 - Exception in method signature in interface 在每种方法中添加引发签名 - Add throws signature in every method 为什么 throws 是方法签名的一部分 - Why is throws part of the method signature 我们是否需要在throw声明中在方法签名中已经提到的catch块中引发相同的异常 - Do we need to throw the same exception in catch block which is already mentioned in method signature using throws declaration 调用引发异常的方法 - Call a method that throws exception 使用 Kotlin 在方法中抛出异常 - throws Exception in a method with Kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM