简体   繁体   English

我们是否需要在throw声明中在方法签名中已经提到的catch块中引发相同的异常

[英]Do we need to throw the same exception in catch block which is already mentioned in method signature using throws declaration

I have a method in my Java App which throws SQLException . 我的Java应用程序中有一个抛出SQLException Is it necessary to throw the SQLException in the catch block so that the exception is thrown to the calling method where the exception is handled? 是否有必要将SQLException在catch块中,以便将异常抛出到处理该异常的调用方法?

public void insert(Connection conn) throws SQLException
{
    try {
        // my code
    } catch (SQLException s) {
        throw s;
    }
}

If you want to handle the exception yourself and then pass it to the calling method, then yes, you will need to re-throw it: 如果要自己处理该异常,然后将其传递给调用方法,则可以,您将需要重新抛出该异常:

public void insert(Connection conn) throws SQLException {
    try {
        // Your code.
    } catch (SQLException s) {
        // Handle s.
        throw s;
    }
}

If you don't need to do anything with the exception and just want to pass it to the calling method then you can just omit the try - catch and if an exception is thrown the calling method will receive it: 如果您不需要对异常做任何事情,而只想将其传递给调用方法,则可以省略try - catch ,如果引发异常,则调用方法将收到该异常:

public void insert(Connection conn) throws SQLException {
    // Your code.
}

The error occur because you place your code inside a 'try-catch' block, if any exception will happen the catch block handle with it. 发生该错误是因为将代码放置在“ try-catch”块内,如果发生任何异常,catch块句柄也会随之发生。 not send to the calling method. 不发送给调用方法。

You can use the below function to thrown the exception to the calling method 您可以使用以下函数将异常抛出给调用方法

public void insert(Connection conn) throws SQLException {
    //code.
}

If you declare exception in throws clause of the method, then you can: 如果在方法的throws子句中声明异常,则可以:
1) throw exception of declared type. 1)抛出声明类型的异常。
2) throw exception of subtype of declared type. 2)抛出声明类型子类型的异常。
3) swallow the exception. 3)吞下异常。 (Not recommended as you have declared it in signature) (不建议使用,因为您已经在签名中声明了它)
4) throw any other runtime exception. 4)抛出任何其他运行时异常。

Now coming to your example, It would be good to re-throw the exception so that the caller method knows that something wrong happened and take appropriate action (eg if a transaction is running on the Connection object then roll-back the transaction). 现在来看您的示例,最好抛出异常,以便调用方方法知道发生了什么错误并采取适当的措施(例如,如果事务在Connection对象上运行,则回滚该事务)。

暂无
暂无

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

相关问题 如果我们在方法签名中抛出子类型异常,是否可以在方法中抛出Parent类型异常? - Can we throw Parent type exception in the method if we written throws child type exception in the method signature? 在Java catch块中,您如何知道哪个方法/行抛出异常? - In Java catch block, how do you know which method/line throws the exception? 我应该在方法声明中提到哪些异常,以及应该在catch块中捕获哪些异常? - What exceptions I should mention at method declaration, and which exception I should catch in my catch block? 即使在catch块中捕获了异常对象之后,我们还能将异常对象扔给调用方法吗? - can we throw the exception object to calling method even after catching it in the catch block? 如何调用在catch块中引发异常的方法? - How to call a method that throws an exception in catch block? 在 catch 块中调用 void 方法并抛出异常的方法的单元测试 - Unit test for method which calls void method in catch block and throws exception 需要更好地解释 Java 中的 try/catch/throw/throws 异常处理 - Need a better explanation for try/catch/throw/throws exception handing in Java 如何在调用抛出异常的方法时避免在 try catch 块中进行虚拟返回? - How to avoid dummy return in try catch block while a method which throws exception is called? 如何对不抛出任何异常的 catch 块进行单元测试 - How to unit test a catch block which does not throw any exception 在catch块中引发异常是否没有用? - Is it useless to throw an exception in a catch block?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM