简体   繁体   English

我们可以在抛出中使用泛型异常吗?

[英]Can we use generic exception in throws?

I have a silly doubt. 我有一个愚蠢的疑问。 While using throws I know we specify multiple exceptions. 我知道在使用throws我们会指定多个异常。 My query is can we include Exception (generic) also along with other specific exceptions? 我的查询是我们是否可以将Exception (泛型)以及其他特定的异常包括在内? Is that right coding approach? 那是正确的编码方法吗?

public String display() throws IOException, FileNotFoundException, Exception {
    ...
}

Here using Exception is right approach?? 这里使用Exception是正确的方法?

It is legal code. 这是法律法规。

It is a bad idea. 这是一个坏主意。

The reason that it is a bad idea is that the caller now has to handle (or propagate) Exception . 这个主意不好的原因是调用方现在必须处理(或传播) Exception But an Exception could be almost anything. 但是Exception几乎可以是任何东西。 How can you do anything sensible 1 with an exception in an exception handler if you don't know what it could be? 如果您不知道异常可能是什么,那么如何在异常处理程序中执行任何有意义的1处理?

I would argue that if you don't know what the cause of an exception is, the only totally safe thing you can do is to log it and terminate the application. 我会辩称,如果您不知道异常的原因是什么,您唯一可以做的完全安全的事情就是记录该异常并终止该应用程序。 In some contexts you could try to log the exception, "fail" the request and carry on with the next one. 在某些情况下,您可以尝试记录异常,使请求“失败”并继续下一个请求。 That may be a sensible strategy, if service availability is the highest priority, and the system is designed to be robust (eg idempotent requests, transactional implementation). 如果服务可用性是最高优先级,并且系统设计为健壮的(例如,幂等请求,事务实现),那么这可能是一个明智的策略。 However, there is a risk that this approach will increase the damage. 但是,存在这种方法会增加损害的风险。 But the problem with throwing Exception is that you make it much harder to discriminate between errors that are recoverable and those that are not. 但是,引发Exception的问题是,您很难区分可恢复的错误和不可恢复的错误。

It is legal Java code. 它是合法的Java代码。

First part for the usage of Exception in throws declaration. throws声明中使用Exception第一部分。

It should seldom be written in production code, before it suggests that the method could throw absolutely any checked exception, which is quite uncommon. 在建议该方法可以绝对抛出任何已检查的异常之前,它几乎不应该用生产代码编写,这种情况很少见。

But it is currently used in Junit tests. 但目前在Junit测试中使用。 The aim of a test function is to test one single feature, and should be as simple as possible because it is expected to not contain logic errors. 测试功能的目的是测试一个功能,并且应该尽可能简单,因为它应该不包含逻辑错误。 So it is common to write the test as: 因此,通常将测试编写为:

@Test
public void testFeature() throws Exception {
    //actual test that can call any function declaring checked exception
}

Here this just means that you do not want to worry for possible exception declared in functions used in the test body. 在这里,这仅意味着您不想担心测试主体中使用的函数中声明的可能的异常。


But your code declares Exception in addition to other exception (sub-)classes. 但是,除了其他异常(子)类之外,您的代码还声明了Exception。 That is useless because all exception classes are subclasses of Exception , thus IOException and FileNotFoundException are Exception s. 这是没有用的,因为所有异常类都是Exception子类,因此IOExceptionFileNotFoundException 都是 Exception

So even if legal, this does not make sense: if you put Exception in throws declaration, you should remove all other exception classes because they are already contained in the Exception declaration. 因此,即使合法,也没有意义:如果将Exception放入throws声明中,则应删除所有其他异常类,因为它们已包含在Exception声明中。

In my opinion method can throw generic Exception, when you business logic and your pipeline works like application has to continue work even after fatal error, log this error as FFDC and continue works. 在我看来,方法可能会引发通用异常,当您的业务逻辑和管道工作时,即使致命错误发生后,应用程序也必须继续工作,将此错误记录为FFDC并继续工作。 If your application has to shutdown after fatal eror, then your method has not to throw generic Exception. 如果您的应用程序必须在致命错误后关闭,则您的方法不必抛出通用Exception。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM