何时使用catch和何时使用throws进行异常处理 [英] When to use catch and when to use throws in the exception handling

查看:172
本文介绍了何时使用catch和何时使用throws进行异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题不在于异常处理语法,而是在传播方法的过程中写入catch块的正确位置是正确的。

  public boolean validateUser(String username,String password)throws SQLException {
Connection conn = dataSource.getConnection();
boolean result = false;
PreparedStatement pstmt = conn.prepareStatement(SELECT * FROM USERS WHERE USERNAME =?AND PASSWORD =?);
pstmt.setString(1,username);
pstmt.setString(2,密码);
result = pstmt.executeQuery()。next();
conn.close();
返回结果;
}

假设 method1()调用上面的方法调用 method2() method2()在上面的方法中,如果我处理异常,我必须返回一个值。假设我已经在catch块之后返回了 false method2()像用户名或密码这样的误会是错误的。 / p>

如果我没有处理, method2()将不会收到任何值,它的catch块将执行相同问题会发生在 method1()中。



现在可以定义哪里可以有效地处理异常?

解决方案

您应该 catch 您可以从错误状态合理地恢复。在其他情况下,您应该将其传播给您的呼叫者,并希望他能够以有意义的方式处理它。如果没有人调用堆栈将能够处理它,终止应用程序可能是正确的事情发生。



假设您正在使用图标编写图形用户界面。您有一种方法

 图标loadIcon(String filename)throws IOException; 

从磁盘文件加载图标的图像。如果这个I / O操作失败,则没有明显的方法可以从该错误中恢复loadIcon 。最好让异常传播。



在其他地方,您还有另一种方法

  void buildGUI(); 

填充用户界面。它将利用 loadIcon 根据当前选择的图标主题获取各种按钮的图标。如果加载一个图标失败,这将导致整个GUI构建过程崩溃。在这里,我们应该可以捕获异常并尝试使用后备图标或显示一个空白按钮。这将允许用户仍然使用该应用程序,并可能意识到某些图标丢失,从而进行修复。


The question is not about exception handling syntax, but it is all about which is the right place to write catch block for an exception in its journey over methods through propagation.

public boolean validateUser(String username, String password) throws SQLException {
    Connection conn = dataSource.getConnection();
    boolean result = false;
    PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM USERS WHERE   USERNAME=? AND PASSWORD=?");
    pstmt.setString(1, username);
    pstmt.setString(2, password);
    result = pstmt.executeQuery().next();
    conn.close();
    return result;
}

Assume method1() called method2() and method2() called above method. In the above method if I handle the exception, I have to return a value. Let's assume that I have returned false after catch block, method2() misunderstands like the username or password is wrong.

If I am not handling, method2() will not receive any value and it's catch block will execute and the same problem would occur in method1().

Now can you define where can I effectively handle the exception?

解决方案

You should catch an exception only if you can reasonably recover from the error condition. In every other case, you should propagate it up to your caller and hope that he might be able to handle it in a meaningful way. If nobody up the call stack will be able to handle it, termination of the application is probably the correct thing to happen.

Suppose you are writing a graphical user interface with icons. You have one method

Icon loadIcon(String filename) throws IOException;

that loads the icon's image from a disk file. If this I/O operation fails, there is no obvious way loadIcon could recover from that error. It is probably best to let the exception propagate.

Somewhere else, you have another method

void buildGUI();

that populates the user interface. It will make use of loadIcon to get icons for the various buttons based on the currently selected icon theme. If loading one icon fails, this would be a poor reason to crash the whole GUI building process. Here, we should probably catch the exception and try using a fallback icon or display an empty button. This will allow the user to still use the application and perhaps realize that some icons are missing and thus go and fix their installation.

这篇关于何时使用catch和何时使用throws进行异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆